typescript 使用扩展运算符更新包含对象的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44524121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update Array containing objects using spread operator
提问by developer
I have an array containing objects in javascript / typescript.
我有一个包含 javascript/typescript 对象的数组。
let array = [{id:1,name:'One'}, {id:2, name:'Two'}, {id:3, name: 'Three'}]
How can I update name of the second element (with id 2) and copy the array to a new array using javascript spread (...) operator?
如何更新第二个元素的名称(id 为 2)并使用 javascript spread (...) 运算符将数组复制到新数组?
回答by George
You can use a mix of .map
and the ...
spread operator
You can set the value after you've created your new array
您可以在创建新数组后设置该值
let array = [{id:1,name:'One'}, {id:2, name:'Two'}, {id:3, name: 'Three'}];
let array2 = array.map(a => {return {...a}})
array2.find(a => a.id == 2).name = "Not Two";
console.log(array);
console.log(array2);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Oryou can do it in the .map
或者你可以在.map
let array = [{id:1,name:'One'}, {id:2, name:'Two'}, {id:3, name: 'Three'}];
let array2 = array.map(a => {
var returnValue = {...a};
if (a.id == 2) {
returnValue.name = "Not Two";
}
return returnValue
})
console.log(array);
console.log(array2);
.as-console-wrapper { max-height: 100% !important; top: 0; }
回答by Shinoy Babu
We can use
我们可以用
let array = [{id:1,name:'One'}, {id:2, name:'Two'}, {id:3, name: 'Three'}];
let array2 = [...array]
array2.find(a => a.id == 2).name = "Not Two";
console.log(array2);
回答by Arpan Banerjee
You can simply use map()
and change the element there.
here is the code---
您可以简单地map()
在那里使用和更改元素。这是代码---
array_copy = array.map((element) => {
console.log(element.id);
if (element.id === 2) {
element.name = "name changed";
}
return element;
});
console.log(array_copy);
Here the main array also gets modified, as elements inside the array are objects and it references to the same location even in the new array.
这里主数组也被修改,因为数组内的元素是对象,即使在新数组中它也引用相同的位置。
回答by David
There are a few ways to do this. I would suggest using Array.map
:
有几种方法可以做到这一点。我建议使用Array.map
:
let new_array = array.map(element => element.id == 2 ? {...element, name : 'New Name'} : element);
or with Object.assign
:
或与Object.assign
:
let new_array = array.map(element => element.id == 2 ? Object.assign({}, element, {name : 'New Name'}) : element);
Map returns a new array, so you shouldn't need the array spread operator.
Map 返回一个新数组,因此您不需要数组扩展运算符。
回答by Shashank Singh
let array = [{id:1,name:'One'}, {id:2, name:'Two'}, {id:3, name: 'Three'}];
let array2 =[...array.slice(0, 0), Object.assign({}, array[0], {
name:'new one' //change any property of idx
}),...array.slice(0 + 1)]
console.log(array);
console.log(array2);
[...array.slice(0, idx), Object.assign({}, array[idx], {
x:new_x //change any property of idx
}),...array.slice(idx + 1)]
回答by Elli Avram
let array = [
{ id: 1, name: "One", location: { lat: 23.2223, lng: 56.2214 } },
{ id: 2, name: "Two", location: { lat: 23.2223, lng: 56.2214 } },
{ id: 3, name: "Three", location: { lat: 23.2223, lng: 56.2214 } },
];
I have this array like this what should I do? tell me please, above answer cannot mutate the location object.
我有这样的数组我该怎么办?请告诉我,上面的答案不能改变位置对象。