所以)。这将删除所有重复项。但是我无法弄清楚删除彼此相邻的重复项的逻辑。
例如:input :('FFNNbbffnnNN'); 输出:[F,N,b,f,n,N];
var uniqueInOrder = function(iterable){
var newArr =[];
var len = iterable.length;
for(var i = 0; i < len ; i ++){
if( newArr.indexOf(iterable[i]) === -1){
newArr.push(iterable[i])
}
}
return newArr;
}
uniqueInOrder('ffssSnnsS');
在这里,我尝试了一点.. meh ..请求小费。谢谢!
var uniqueInOrder = function(iterable){
var newArr =[];
var len = iterable.length;
var first = iterable[0];
for(var i = 0; i < len ; i ++){
if( newArr.indexOf(first) !== newArr.indexOf(first + 1){
newArr.push(iterable[i])
}
}
return newArr;
}
uniqueInOrder('ffssSnnsS');
var nums = [1,2,3,3,4,5,5,6,7,7,8];
nums.forEach(function(num,index){
// Is the current index < the amount of itmes in the array
// and, if so, is the current item equal to the next item?
if(index < nums.length && num === nums[index + 1]){
nums.splice(index,1); // Remove the current item
}
});
console.log(nums);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句