[英]Copy array of objects and make changes without modifying original array
I have an array of objects.我有一个对象数组。 I would like to deep copy the array of objects and make some changes to each object.我想深度复制对象数组并对每个 object 进行一些更改。 I want to do this without modifying the original array or original objects that were in that array.我想在不修改原始数组或该数组中的原始对象的情况下执行此操作。
This is the way I have done it.我就是这样做的。 However, being new to JavaScript I want to make sure this is a good approach.但是,作为 JavaScript 的新手,我想确保这是一个好方法。
Is there a better way to do this?有一个更好的方法吗?
const users =
[
{
id : 1,
name : 'Jack',
approved : false
},
{
id : 2,
name : 'Bill',
approved : true
},
{
id : 3,
name : 'Rick',
approved : false
},
{
id : 4,
name : 'Rick',
approved : true
}
];
const users2 =
users
.map(
(u) =>
{
return Object.assign({}, u);
}
)
.map(
(u) =>
{
u.approved = true;
return u;
}
);
console.log('New users2 array of objects:')
console.log(users2);
console.log('This was original users array is untouched:')
console.log(users);
Output: Output:
New users2 array of objects:
[ { id: 1, name: 'Jack', approved: true },
{ id: 2, name: 'Bill', approved: true },
{ id: 3, name: 'Rick', approved: true },
{ id: 4, name: 'Rick', approved: true } ]
This was original users array is untouched:
[ { id: 1, name: 'Jack', approved: false },
{ id: 2, name: 'Bill', approved: true },
{ id: 3, name: 'Rick', approved: false },
{ id: 4, name: 'Rick', approved: true } ]
For a single pass, you could use Object.assign
with the changed property as well.对于单次传递,您也可以将Object.assign
与更改的属性一起使用。
const users = [{ id: 1, name: 'Jack', approved: false }, { id: 2, name: 'Bill', approved: true }, { id: 3, name: 'Rick', approved: false }, { id: 4, name: 'Rick', approved: true }]; const users2 = users.map(u => Object.assign({}, u, { approved: true })); console.log(users2); console.log(users);
.as-console-wrapper { max-height: 100% !important; top: 0; }
UPDATE with spreading properties.更新传播属性。
const users = [{ id: 1, name: 'Jack', approved: false }, { id: 2, name: 'Bill', approved: true }, { id: 3, name: 'Rick', approved: false }, { id: 4, name: 'Rick', approved: true }]; const users2 = users.map(u => ({ ...u, approved: true })); console.log(users2); console.log(users);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Yes that looks good.是的,看起来不错。 You could also perform the modification when you are cloning, in order to avoid mapping over the array twice.您也可以在克隆时执行修改,以避免两次映射数组。
const users2 = users.map((u) => {
const copiedUser = Object.assign({}, u);
copiedUser.approved = true;
return copiedUser;
});
I prefer JSON.stringify and JSON.parse我更喜欢 JSON.stringify 和 JSON.parse
var users = [ { id: 1, name: 'Jack', approved: false },
{ id: 2, name: 'Bill', approved: true },
{ id: 3, name: 'Rick', approved: false },
{ id: 4, name: 'Rick', approved: true } ];
// user2 will be copy of array users without reference
var users2 = JSON.parse(JSON.stringify(users));
There are a few ways to copy a array in javascript, i believed that the most used are:有几种方法可以在 javascript 中复制数组,我认为最常用的是:
The slice function will return a portion (or all content) of a given array as a new array, based at a begin and end index (begin and end index are optional): slice 函数将返回给定数组的一部分(或所有内容)作为新数组,基于开始和结束索引(开始和结束索引是可选的):
const a = [1,2,3,4,5,6,7,8,9] /* * Only begin index */ const b = a.slice(2) console.log(b) //Will Print [3,4,5,6,7,8,9] /* * Begin index and end index */ const c = a.slice(5,8) console.log(c) //Will Print [6,7,8] /* * No indexes provided */ const d = a.slice() console.log(d) //Will print [1,2,3,4,5,6,7,8,9]
Array.from() is a function that will create a new array from an array-like or iterable parameters. Array.from() 是一个函数,它将从类似数组或可迭代的参数创建一个新数组。
const a = Array.from('bar'); console.log(a) //Will Print ["b","a","r"] const b = ["for","bar"]; const c = Array.from(b); console.log(c) //Will print ["for","bar"]
In React JS
I tried most ways of copying and modifying the newly copied array but it still modified the original array.在React JS
中,我尝试了大多数复制和修改新复制的数组的方法,但它仍然修改了原始数组。 The methods I tried are slice
Array.from
for loop
spread operator
, all this ended up modifying the original array.我尝试的方法是slice
Array.from
for loop
spread operator
,所有这些最终都修改了原始数组。 The solution was, stringify
the original array and assign it to a new variable, then parse
the variable to get a fresh copy without reference as stated by @Roman Yakoviv above.解决方案是,将原始数组stringify
并将其分配给一个新变量,然后parse
该变量以获得一个新的副本,而无需引用,如上面@Roman Yakoviv 所述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.