添加方法
let arr= [1,2,3,2]
let tow = []
for(let item of arr){ arr.indeof(1) < 0 ? tow.push(item) :‘’ }
let arr= [1,2,3,2]
let tow = {}
let newArray=[]
for(let item of arr){ tow.item = ''}
for(let index in tow ){ newArray.push(item)}
这种如果是对象话列入判断名称
就以名称为key将对象为value
删除方法
function unique(arr) { var array = arr; var len = array.length; array.sort(function (a, b) { //排序后更加方便去重 return a - b; }) function loop(index) { if (index >= 1) { if (array[index] === array[index - 1]) { array.splice(index, 1); } loop(index - 1); //递归loop,然后数组去重 } } loop(len - 1); return array; } var arr = [1, 1, 'true', 'true', true, true, 15, 15, false, false, undefined, undefined, null, null, NaNaN, 'NaN', 0, 0, 'a', 'a', {}, {}]; console.log(unique(arr))
es6方法
let arr =[1,2,3] new Set(arr)