关卡详解
var arr = [2,3,4,2,3,5,6,4,3,2];
var unique = function(arr) {
var result = [];
arr.forEach(function(item){
if(result.indexOf(item) < 0){
result.push(item);
}
});
return result;
}
console.log(unique(arr));
var arr = [2,3,4,[2,3,4,5],3,5,[2,3,4,2],4,3,6,2];
var unique = function(arr){
var result = [];
arr.forEach(function(item) {
if (Array.isArray(item)) {
item.forEach(function(i) {
if (result.indexOf(i) < 0) {
result.push(i);
}
});
} else {
if (result.indexOf(item) < 0) {
result.push(item);
}
}
});
return result;
}
console.log(unique(arr));
var arr = [2,3,4,[2,3,[2,3,4,2],5],3,5,[2,3,[2,3,4,2],2],4,3,6,2];
var unique = function(arr) {
var result = [];
var f = function(i) {
i.forEach(function(item) {
if (Array.isArray(item)) {
f(item);
} else {
if (result.indexOf(item) < 0) {
result.push(item);
}
}
});
};
f(arr);
return result;
}
console.log(unique(arr));