当前位置:网站首页>JS merge multiple objects and remove duplicates
JS merge multiple objects and remove duplicates
2022-06-24 08:33:00 【User 2323866】
Method 1 :
let o1 = { a: 1, b: 2 };
let o2 = { c: 4, d: 5 };
let o3 = {...o1, ...o2};//{ a: 1, b: 2, c: 4, d: 5} If there are duplicates key, Then the following will overwrite the previous value
let o1 = { a: 1, b: 2 };
let o2 = { c: 4, b: 5 };
let o3 = {...o1, ...o2};//{ a: 1, b: 5, c: 4}Method 2 :
Object.assign Method is used for merging objects , Put the source object (source) All enumerable properties of , Copy to target object (target).
const target = { a: 1 };
const source1 = { b: 2 };
const source2 = { c: 3 };
Object.assign(target, source1, source2);
target // {a:1, b:2, c:3}Object.assign The first parameter of the method is the target object , Later parameters are all source objects .
Be careful , If the target object has the same property as the source object , Or multiple source objects have properties with the same name , Then the following properties will overwrite the previous properties .
const target = { a: 1, b: 1 };
const source1 = { b: 2, c: 2 };
const source2 = { c: 3 };
Object.assign(target, source1, source2);
target // {a:1, b:2, c:3}边栏推荐
猜你喜欢

【关于运维和网工的差别,一文说透】

【微服务~Nacos】Nacos服务提供者和服务消费者

2021-03-09 COMP9021第七节课笔记

Maya re deployment

Redis的Cluster集群数据倾斜

Future trends in automated testing

Live broadcast review | detailed explanation of koordinator architecture of cloud native hybrid system (complete ppt attached)

5分钟,客服聊天处理技巧,炉火纯青

LabVIEW查找n个元素数组中的质数

C language_ Love and hate between string and pointer
随机推荐
Use of swift basic closure /block (source code)
All you know is the test pyramid?
Which is the first poem of Tang Dynasty?
13 -- remove invalid parentheses
RCNN、Fast-RCNN、Faster-RCNN介绍
Common date formatter and QT method for obtaining current time
A preliminary study of IO model
pyQt 中 QMenu 响应
longhorn安装与使用
05-ubuntu安装mysql8
[introduction to point cloud dataset]
Easydss anonymous live channel data volume instability optimization scheme sharing
Robot acceleration level task priority inverse kinematics
12--合并两个有序链表
Scénarios d'utilisation de la promesse
Question 3 - MessageBox pop-up box, modify the default background color
基金的募集,交易与登记
2021-03-09 COMP9021第七节课笔记
OpenCV to realize the basic transformation of image
The JS macro of WPS implements the separation method of picture text in the same paragraph