当前位置:网站首页>js 对象数组去重
js 对象数组去重
2022-07-23 21:30:00 【伟笑】
1、使用reduce()方法对象数组去重
通过数组的reduce()方法对数组的对象从左到右进行处理,定义一个空对象obj{};
如果数组当前对象的name不在obj{},就将当前对象的name加入到空对象,并将当前对象push到积累变量total数组里;
如果当前对象的name存在obj{},则跳过push操作。
//reduce第一个参数是遍历需要执行的函数,第二个参数是item的初始值
var arr = [{
"id":1,"name":"张三"},{
"id":2,"name":"李四"},{
"id":3,"name":"王五"},{
"id":2,"name":"张三"}];
var obj = {
};
arr = arr.reduce(function(item, next) {
obj[next.id] ? '' : obj[next.id] = true && item.push(next);
return item;
}, []);
console.log(arr);
2、利用set去重
unique(arr) {
const res = new Map();
return arr.filter((a) => (
(!res.has(a.planNo) && res.set(a.planNo, 1))&& (!res.has(a.jobId) && res.set(a.jobId, 1))));
}
边栏推荐
- 集群聊天服务器:如何解决跨服务器通信问题 | redis发布-订阅
- Now I don't know how to synchronize at all
- query中的customer exit客户出口变量
- [complex overloaded operator]
- Vite3 learning records
- It's good to change jobs for a while, and it's good to change jobs all the time?
- Boost Filesystem使用手册
- 博客总排名为918
- Why cluster chat server introduces load balancer
- 合宙ESP32C3硬件配置信息串口打印輸出
猜你喜欢

合宙ESP32C3硬件配置信息串口打印輸出

Chapter 2 Regression

221. Largest square ● &1277. Square submatrix with statistics all 1 ● ●

博客总排名为918

Vite3 learning records

Day109. Shangyitong: integrate Nacos, hospital list, drop-down list query, hospital online function, hospital details query

宇树A1机器狗手势控制

Be a professional software craftsman

Typescript Basics

Qt桌面白板工具其一(解决曲线不平滑的问题——贝塞尔曲线)
随机推荐
Cmake learning
scala編程(初級)
Basic knowledge of mobile phone testing
【arxiv】第一次上传论文小记
prime_ series_ level-1
Openlayers instance accessible map accessible map
Minimum spanning tree: prim
2022-7-23 12点 程序爱生活 小时线顶背离出现,保持下跌趋势,等待反弹信号出现。
集群聊天服务器:工程目录的创建
How to implement desktop lyrics in pyqt
WinDbg practice -- Introduction
MySQL数据库索引
An interview question about common pitfalls in golang for range
如何在面試中介紹自己的項目經驗
[wechat applet] do you know about applet development?
Kuberntes cloud native combat VI uses rook to build CEPH cluster
One of QT desktop whiteboard tools (to solve the problem of unsmooth curve -- Bezier curve)
集群聊天服務器:數據庫錶的設計
Oom mechanism
[attack and defense world web] difficulty four-star 12 point advanced question: confusion1