我有一个数组,其中包含javascript /打字稿中的对象。
let array = [
{ id: 1, name: "One", location: { lat: 23.2223, lng: 56.2214 } },
{ id: 2, name: "Two", location: { lat: 23.2226, lng: 56.2214 } },
{ id: 3, name: "Three", location: { lat: 23.2223, lng: 56.2214 } },
];
如何更新第二个元素的位置(lat 23.2226)并使用javascript传播(...)运算符将数组复制到新数组?
答案 0 :(得分:0)
let array = [
{ id: 1, name: "One", location: { lat: 23.2223, lng: 56.2214 } },
{ id: 2, name: "Two", location: { lat: 23.2226, lng: 56.2214 } },
{ id: 3, name: "Three", location: { lat: 23.2223, lng: 56.2214 } },
];
array.find((d) => (d['name'] == 'Two'))['location']['lat'] = 23.000
console.log(array)
/*[
{ id: 1, name: 'One', location: { lat: 23.2223, lng: 56.2214 } },
{ id: 2, name: 'Two', location: { lat: 23, lng: 56.2214 } },
{ id: 3, name: 'Three', location: { lat: 23.2223, lng: 56.2214 } }
]*/