Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

js数组对象去重

 let arr = [
    {
        pk:'1',
        name:'张',
        age:'13'
    },
    {
        pk:'2',
        name:'张',
        age:'23'
    },
    {
        pk:'1',
        name:'张',
        age:'13'
    }
    ]

有什么快捷的方式可以去除数组里一模一样的对象吗?

对象1和3是一样的


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

用 Lodash 一个函数就能搞定 uniqWith
https://www.lodashjs.com/docs...

关键是找到计算相同的算法,如果没啥特别的,用 Lodash 的 isEquals 也挺方便
https://www.lodashjs.com/docs...

import _ from "lodash";

const result = _.uniqWith(arr, _.isEqual);

补充个参考链接2020-09-17数组去重(JavaScript 为例)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...