-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9SpreadOperatorOnObject.js
More file actions
32 lines (26 loc) · 987 Bytes
/
9SpreadOperatorOnObject.js
File metadata and controls
32 lines (26 loc) · 987 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Spread Operator On Object
// NOTE
// - When both objects has same name but values are different and you combine them together to make one object.At that time even if the names are same but value will be consider(taken) from the only last object. Because even though both have same name but latest update value will be considered from the last object.
const object1 = {
name1: "value1",
name2: "value2",
name3: "value3",
sameName: "sameValue1",
};
const object2 = {
name4: "value4",
name5: "value5",
name6: "value6",
sameName: "sameValue2",
};
// Add both objects into the new object
const object3 = { ...object1, ...object2 };
console.log(object3);
// Add both objects into the one new object and adding name : value pair
const object4 = { ...object1, ...object2, name7: "value7" };
// When array is added into the object at that time array index will be name and element will be value
const arr = [1, 2, 3];
const object5 = {
...arr,
};
console.log(object5);