gpt4 book ai didi

javascript - 传播运营商取代值(value)?

转载 作者:行者123 更新时间:2023-11-30 07:03:57 25 4
gpt4 key购买 nike

为什么传播运算符在这里用 'HB woodlawn' 替换 'Stratford' 的值?这是如何工作的?

const editName = (oldName, name, arr) =>
arr.map(item => {
if (item.name === oldName) {
return {
...item,
name
}
} else {
return item
}
})

let schools = [
{ name: "Yorktown"},
{ name: "Stratford" },
{ name: "Washington & Lee"},
{ name: "Wakefield"}
]
let updatedSchools = editName("Stratford", "HB Woodlawn", schools)
console.log( updatedSchools[1] ) // { name: "HB Woodlawn" }
console.log( schools[1] ) // { name: "Stratford" },

最佳答案

归根结底是这样的:

const oldObject = { name: 'Stratford' };
const newObject = { ...oldObject, name: 'HB Woodlawn' };

您可以将其视为将 oldObject 的属性扩展到新的对象字面量中:

const newObject = { name: 'Stratford', name: 'HB Woodlawn' };

文字中具有相同键的属性获取最后一个的值。

关于javascript - 传播运营商取代值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44982586/

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