A lightweight JavaScript utility allowing deep copy-by-value of nested objects, arrays and arrays of objects.
Development size: 1.06 KB
Minified size: 347 bytes
let firstArray = [1, 2, 3]
let secondArray = clone(firstArray)
secondArray[0] = 5
console.log('firstArray => ', firstArray) // [1, 2, 3]
console.log('secondArray => ', secondArray) // [5, 2, 3]
let firstArray = [
{
id: '103',
name: 'Peter'
},
{
id: '214',
name: 'Eve'
}
]
let secondArray = clone(firstArray)
secondArray[0].name = 'John'
console.log('firstArray => ', firstArray)
console.log('secondArray => ', secondArray)
let firstStudent = {
id: 103,
name: 'Ben',
classes: ['Maths', 'Science', 'English language']
}
let secondStudent = clone(firstStudent)
secondStudent.classes[0] = 'Psychology'
console.log('firstStudent => ', firstStudent)
console.log('secondStudent => ', secondStudent)
let firstStudent = {
id: 103,
name: 'Ben',
subjects: {
groupDke: {
science: 'B',
maths: 'C'
},
groupOpe: {
foo: 'bar'
}
}
}
let secondStudent = clone(firstStudent)
secondStudent.subjects.groupDke.maths = 'B'
console.log('firstStudent => ', firstStudent)
console.log('secondStudent => ', secondStudent)