メモとして。
const obj = [
{
name: 'taro',
age: 24
},
{
name: 'sato',
age: 22
},
]
const copyObj = obj.map(person => ({name : person.name ,age : person.age}))
for(let i = 0; i < copyObj.length; i++){
copyObj[i].name = "keiko";
copyObj[i].age = i + 20;
}
console.log(obj);
console.log(copyObj);
結果
[[object Object] {
age: 24,
name: "taro"
}, [object Object] {
age: 22,
name: "sato"
}]
[[object Object] {
age: 20,
name: "keiko"
}, [object Object] {
age: 21,
name: "keiko"
}]
配列も同様
const obj = [
{
answer: [
[1,1,1],
[1,1,1]
],
remain: [2,2,2]
}
]
const copyObj = obj.map(list => (
{
answer : [...list.answer],
remain : list.remain
}
)
);
copyObj[0].answer[0] = [1,2,3];
copyObj[0].answer[1] = [4,5,6];
console.log(obj);
console.log(copyObj);
結果
[[object Object] {
answer: [[1, 1, 1], [1, 1, 1]],
remain: [2, 2, 2]
}]
[[object Object] {
answer: [[1, 2, 3], [4, 5, 6]],
remain: [2, 2, 2]
}]