gpt4 book ai didi

Javascript更新对象属性值

转载 作者:行者123 更新时间:2023-11-30 09:18:09 26 4
gpt4 key购买 nike

我正在尝试更新对象属性:

   data : [
{"id":1,"rp":"2426","cr":"11-11"},
{"id":1,"rp":"1119","cr":"19-21"},
{"id":1,"rp":"3453","cr":"23-81"}
]

新对象将更新如下。

NewData: [
{"id":1,"rp":"2426","cr":"11/11"},
{"id":1,"rp":"1119","cr":"19/21"},
{"id":1,"rp":"3453","cr":"23/81"}
]

我希望为所有值更新对象 cr 属性,例如使用 javascript.replace() 方法,我会做 replace("-","/")。

最佳答案

这只是 Array#mapString#replace 的简单用法:

ES2018

const data = [{"id":1,"rp":"2426","cr":"11-11"},{"id":1,"rp":"1119","cr":"19-21"},{"id":1,"rp":"3453","cr":"23-81"}]

const r = data.map(({ cr, ...rest }) => ({ cr: cr.replace('-', '/'), ...rest }));

console.log(r);

关于Javascript更新对象属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53563021/

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