当前位置:网站首页>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}边栏推荐
猜你喜欢

longhorn安装与使用

1279_ Vsock installation failure resolution when VMware player installs VMware Tools

2021-03-04 COMP9021第六节课笔记

ZUCC_编译语言原理与编译_实验05 正则表达式、有限自动机、词法分析

Question 4 - datepicker date selector, disabling two date selectors (start and end dates)

io模型初探

Blue Bridge Cup_ Queen n problem

小样本故障诊断 - 注意力机制代码 - BiGRU代码解析实现

For a detailed explanation of flex:1, flex:1

ZUCC_ Principles of compiling language and compilation_ Experiment 05 regular expression, finite automata, lexical analysis
随机推荐
QPS, TPS, concurrent users, throughput relationship
2021-03-04 COMP9021第六节课笔记
Question 3 - MessageBox pop-up box, modify the default background color
longhorn安装与使用
jwt(json web token)
Fund raising, trading and registration
LabVIEW finds prime numbers in an array of n elements
(pkcs1) RSA public private key PEM file parsing
ZUCC_ Principles of compiling language and compilation_ Experiment 08 parsing LR parsing
ZUCC_编译语言原理与编译_实验04 语言与文法
[real estate opening online house selection, WiFi coverage temporary network] 500 people are connected to WiFi at the same time
About the iframe anchor, the anchor is offset up and down, and the anchor has page display problems Srcdoc problem of iframe
WPS的JS宏实现图片正文在同一段落的分离方法
Pat 1157: school anniversary
2021-03-09 COMP9021第七节课笔记
贷款五级分类
App Startup
Export MySQL database to xxx SQL, set xxx The SQL file is imported into MySQL on the server. Project deployment.
[introduction to point cloud dataset]
5 minutes, excellent customer service chat handling skills