Nested Data Structures Flashcards
1
Q
Array shallow copy method
A
array.slice() or you can use […array]
2
Q
Object shallow copy
A
Object.assign({}, obj)
3
Q
Deep copy with plain objects (any object, including arrays, that only has primitives, arrays, and plain objects as elements.)
A
let serializedArr = JSON.stringify(arr);
let deepCopiedArr = JSON.parse(serializedArr);
4
Q
Deep copy with objects
A
let deepCopy = {};
for (const key in obj) {
deepCopy[key] = […obj[key]]
}