An array of
Use the object. The assign
- Object.assign([], a, b)
let a = [1.2]
let b = [3.4]
let c = Object.assign([], a, b)
Copy the code
value | console.log() | a[1] = 6 | b[1] = 6 | c[1] = 5 |
a | [1, 2] | [1, 6] | [1, 2] | [1, 2] |
b | [3, 4] | [3, 4] | [3, 6] | [3, 4] |
c | [3, 4] | [3, 4] | [3, 4] | [3, 5] |
The value of A is overwritten, and changing the values of A, B, and C respectively does not affect the other values
2.Object.assign(a, b)
let a = [1.2]
let b = [3.4]
let c = Object.assign(a, b)
Copy the code
value | console.log() | a[1] = 6 | b[1] = 6 | c[1] = 5 |
a | [3, 4] | [3, 6] | [3, 4] | [3, 5] |
b | [3, 4] | [3, 4] | [3, 6] | [3, 4] |
c | [3, 4] | [3, 6] | [3, 4] | [3, 5] |
The value of A is overwritten. Changing the value of A will affect the value of C synchronously. Changing the value of B has no effect on a and C, but changing the value of C will affect the value of A synchronously
Using the expansion operator…
- […a, …b]
let a = [1.2]
let b = [3.4]
let c = [...a, ...b]
Copy the code
value | console.log() | a[1] = 6 | b[1] = 6 | c[1] = 5 |
a | [1, 2] | [1, 6] | [1, 2] | [1, 2] |
b | [3, 4] | [3, 4] | [3, 6] | [3, 4] |
c | [1, 2, 3, 4] | [1, 2, 3, 4] | [1, 2, 3, 4] | [1, 5, 3, 4] |
The values of A will not be overwritten, and changing the values of A, B, and C will not affect the other values
- […[],…a, …b]
let a = [1.2]
let b = [3.4]
let c = [...[],...a, ...b]
Copy the code
value | console.log() | a[1] = 6 | b[1] = 6 | c[1] = 5 |
a | [1, 2] | [1, 6] | [1, 2] | [1, 2] |
b | [3, 4] | [3, 4] | [3, 6] | [3, 4] |
c | [1, 2, 3, 4] | [1, 2, 3, 4] | [1, 2, 3, 4] | [1, 5, 3, 4] |
The values of A will not be overwritten, and changing the values of A, B, and C will not affect the other values
object
Use the Object. The assign
- Object.assign({}, a, b)
let a = { 1: 'aa'.2: 'bb' }
let b = { 1: 'cc'.4: 'dd' }
let c = Object.assign({}, a, b)
Copy the code
value | console.log() | a[1] = ‘aaa’ | b[1] = ‘ccc’ | c[1] = ‘ccc’ |
a | {1: “aa”, 2: “bb”} | {1: “aaa”, 2: “bb”} | {1: “aa”, 2: “bb”} | {1: “aa”, 2: “bb”} |
b | {1: “cc”, 4: “dd”} | {1: “cc”, 4: “dd”} | {1: “ccc”, 4: “dd”} | {1: “cc”, 4: “dd”} |
c | {1: “cc”, 2: “bb”, 4: “dd”} | {1: “cc”, 2: “bb”, 4: “dd”} | {1: “cc”, 2: “bb”, 4: “dd”} | {1: “ccc”, 2: “bb”, 4: “dd”} |
1. When a, B, and C are merged, the same attributes of the key are merged, and the last element overwrites the previous element 2. Changing the values of a, B, and C separately does not affect the values of the other elements
- Object.assign(a, b)
let a = { 1: 'aa'.2: 'bb' }
let b = { 1: 'cc'.4: 'dd' }
let c = Object.assign(a, b)
Copy the code
value | console.log() | a[1] = ‘aaa’ | b[1] = ‘ccc’ | c[1] = ‘ccc’ |
a | {1: “cc”, 2: “bb”, 4: “dd”} | {1: “aaa”, 2: “bb”, 4: “dd”} | {1: “cc”, 2: “bb”, 4: “dd”} | {1: “ccc”, 2: “bb”, 4: “dd”} |
b | {1: “cc”, 4: “dd”} | {1: “cc”, 4: “dd”} | {1: “ccc”, 4: “dd”} | {1: “cc”, 4: “dd”} |
c | {1: “cc”, 2: “bb”, 4: “dd”} | {1: “aaa”, 2: “bb”, 4: “dd”} | {1: “cc”, 2: “bb”, 4: “dd”} | {1: “ccc”, 2: “bb”, 4: “dd”} |
2. Changing the value of A affects the value of C synchronously, and changing the value of C also affects the value of A. Changing the value of B has no effect on the values of A and C
Using the expansion operator…
1. {… a, … b}
let a = { 1: 'aa'.2: 'bb' }
let b = { 1: 'cc'.4: 'dd' }
letc = {... a, ... b}Copy the code
value | console.log() | a[1] = ‘aaa’ | b[1] = ‘ccc’ | c[1] = ‘ccc’ |
a | {1: “aa”, 2: “bb”} | {1: “aaa”, 2: “bb”} | {1: “aa”, 2: “bb”} | {1: “aa”, 2: “bb”} |
b | {1: “cc”, 4: “dd”} | {1: “cc”, 4: “dd”} | {1: “ccc”, 4: “dd”} | {1: “cc”, 4: “dd”} |
c | {1: “cc”, 2: “bb”, 4: “dd”} | 1: “cc”, 2: “bb”, 4: “dd”} | {1: “cc”, 2: “bb”, 4: “dd”} | {1: “ccc”, 2: “bb”, 4: “dd”} |
1. When a, B, and C are merged, the same attributes of the key are merged, and the last element overwrites the previous element 2. Changing the values of a, B, and C separately does not affect the values of the other elements
- {… {},… a, … b}
let a = { 1: 'aa'.2: 'bb' }
let b = { 1: 'cc'.4: 'dd' }
letc = {... {},... a, ... b}Copy the code
value | console.log() | a[1] = ‘aaa’ | b[1] = ‘ccc’ | c[1] = ‘ccc’ |
a | {1: “aa”, 2: “bb”} | {1: “aaa”, 2: “bb”} | {1: “aa”, 2: “bb”} | {1: “aa”, 2: “bb”} |
b | {1: “cc”, 4: “dd”} | {1: “cc”, 4: “dd”} | {1: “ccc”, 4: “dd”} | {1: “cc”, 4: “dd”} |
c | {1: “cc”, 2: “bb”, 4: “dd”} | {1: “cc”, 2: “bb”, 4: “dd”} | {1: “cc”, 2: “bb”, 4: “dd”} | {1: “ccc”, 2: “bb”, 4: “dd”} |
1. When a, B, and C are merged, the same attributes of the key are merged, and the last element overwrites the previous element 2. Changing the values of a, B, and C separately does not affect the values of the other elements