自学内容网 自学内容网

js-去重多种

let list = [12, 2, 3, 4, 5, 6, 7, 8, 3, 2, 32, 32, 32, 32];

方案1;

let newLlist = list.reduce((per, cur) => {
  if (per.indexOf(cur) === -1) {
    per.push(cur);
  }
  return per;
}, []);

console.log(newLlist);

方案2:

const newLlist1 = [...new Set(list)]
console.log(newLlist1);

方案3:

const newList2= list.filter((item, index) => list.indexOf(item) === index)
console.log(newList2)

方案4:

const newList3= []
list.forEach((item, index) => {
  if (newList3.indexOf(item) === -1) {
    newList3.push(item)
  }
})

console.log(newList3)

原文地址:https://blog.csdn.net/weixin_43991433/article/details/140669996

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!