数组遍历
const ary = [1,6,89,78,,90];
for(let i=0;i<ary.length;i++){
// 过滤不存在,undefined,null
if(!ary[i]) continue;
// 过滤不存在和undefined
if(ary[i]=== undefined) continue;
// 只过滤不存在
if(!(i in ary)) continue;
console.log(ary[i])
}
数组去重
var ary2 = ["apple",1,2,2,2,"banner","apple"];
var unique = [];
for(let i=0;i<ary2.length;i++){
if(unique.indexOf(ary2[i])=== -1) {
unique.push(ary2[i])
}
}
Array.prototype.uniq = function () {
var result = [];
var flag = true;
for(var i =0;i<this.length;i++)
{
if(result.indexOf(this[i])==-1)
{
if(this[i]!=this[i])
{
if(flag)
{
result.push(this[i]);
flag = false;
}
}
else
{
result.push(this[i]);
}
}
}
return result;
}
网友评论