I'm trying to flatten and deep copy of an object.
Here's an example of how I try to do:
const data = {
firstObj: {
data: 'Hi!',
nestedArrayOfObject: [{
name: 'hey',
property: 'object'
}],
},
secondObj: {
name: 'second',
nestedArray: []
},
}
const object = {}
const keys = Object.keys(data)
for (let i = 0; i < keys.length; i += 1) {
const items = Object.keys(data[keys[i]])
for (let j = 0; j < items.length; j += 1) {
object[items[j]] = data[keys[i]][items[j]]
}
}
console.log(object)
As I understand, nested objects are being only linked to the new object, but not cloned.
How to do it correctly without additional libraries?