这样的数组想要去重,求大佬帮忙

新手上路,请多包涵

image.png

阅读 2.2k
3 个回答

如果你是根据id唯一来去重,

const func = (array) => {  
  let name = 'name';  
  let map = new Map();  
  for (let item of array) {  
    if (!map.has(item.id)) {  
      map.set(item.id, item);  
    }  
  }  
  return [...map.values()];  
}  
  
func(array)

你要根据什么去重

你可以自定义一个数组,来根据特定的属性判断是否推入到数组中

function collectById(source) {

    const result = [];

    source.forEach(row => {

    if (!inResult(row, result)) {

        result.push(row);

    }

    });

    return result;

  

    function inResult(row, result) {

    return !!result.find(single => single.id === row.id); // match by id

    }

}

不过建议你下次问问题可以写出自己到实现思路,已经为什么没有达到效果,而不是直接找一个比较好到现成到方案

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进