美文网首页
对象数组优雅去重

对象数组优雅去重

作者: 桃之_夭夭_ | 来源:发表于2020-06-01 18:29 被阅读0次

1、使用Map

const arr = [{id: 1,  name:  'x1'}, {id: 2, name: 'x2' },{id: 1, name: 'x1'}]
console.time('all time')
const map = new Map()
const res = arr.filter(item => !map.has(item.userId) && map.set(item.userId, 1))
console.timeEnd('all time');

2、使用对象key唯一性

const arr = [{id: 1,  name:  'x1'}, {id: 2, name: 'x2' },{id: 1, name: 'x1'}]
console.time('all time')
const obj = {}
 arr.map(item=>{
        obj[item.id] = item
})
const newList = []
for(let key in obj){
  newList.push(obj[key])
}
console.timeEnd('all time');

相关文章

  • 对象数组优雅去重

    1、使用Map 2、使用对象key唯一性

  • 实现数组去重有哪些方式

    简单的数组去重 数组对象去重

  • 数组对象去重方法:

    数组对象去重方法: // 数组对象去重 ```` toRetry = (arr = []) => { let re...

  • 数组去重

    分类 非对象数组去重 对象数组去重 分类一 --- 非对象数组去重 方法一: set(es6常用) 方法二:red...

  • 数组去重的四种方法

    利用双for循环去重 利用对象数组去重 利用对象数组去重并且记录重复次数 通过创建一个新数组进行数组去重

  • js数组去重、对象数组去重

    普通数组去重 一、普通数组去重 方法一:遍历数组法 方法二:排序法 方法三:对象法 对象数组去重 方法一:将对象数...

  • 数组去重

    优雅的数组去重方式

  • Array集结号

    实现数组去重的几种方法 数组去重一 数组去重二 利用数组indexof+push实现数组去重 数组去重三 利用对象...

  • js reduce去重用法

    reduce不仅仅可以数据累加,还可以实现去重效果。 重复次数计算 数组去重 数组对象去重,转为数组 对象去重

  • ES6数组去重

    普通数组去重 方法1 方法2 对象数组去重

网友评论

      本文标题:对象数组优雅去重

      本文链接:https://www.haomeiwen.com/subject/nsidnhtx.html