gpt4 book ai didi

javascript - 使用对象分配添加属性

转载 作者:行者123 更新时间:2023-11-29 17:52:14 24 4
gpt4 key购买 nike

我有以下功能:

addHrefs = (list) => {
const listWithHrefs = list.map((item) => {
item.href = customFunction(item.name);
return item;
});
return listWithHrefs;
}

这里的问题是我正在改变 list 对象。我想知道如何在不改变 list 的情况下添加特定的 (key, value) 对。 Object.assign 是否可行?我该怎么做?

最佳答案

可以用 Object.assign .

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.

您可以将属性值分配给一个新对象并返回它。

addHrefs = list =>
list.map(item => Object.assign({}, item, { href: customFunction(item.name) }));

关于javascript - 使用对象分配添加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42400330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com