【发布时间】:2019-03-23 21:21:12
【问题描述】:
我在试图更改嵌套对象数组中的特定属性值时遇到问题:
const myObj = [
{
"Description":"WA State",
"Data":[
{
"Description":"Years",
"Indicators":[
{
"Year":2018,
"Points":25994,
"Goal":"28000",
}
]
},
{
"Description":"Local Goal",
"Indicators":[
{
"Year":2018,
"Points":25994,
"Goal":"28000",
}
]
},
{
"Description":"Remote Goal",
"Indicators":[
{
"Year":2018,
"Points":55857,
"Goal":"84000",
}
]
}
]
},
{
"Description":"NY State",
"Data":[
{
"Description":"Years",
"Indicators":[
{
"Year":2018,
"Points":21953,
"Goal":"26000",
}
]
},
{
"Description":"Local Goal",
"Indicators":[
{
"Year":2018,
"Points":24195,
"Goal":"25000",
}
]
},
{
"Description":"Remote Goal",
"Indicators":[
{
"Year":2018,
"Points":80857,
"Goal":"90000",
}
]
}
]
}
]
这里我需要将Year属性的所有外观更改为2017,并将Goal属性的所有外观更改为:50000。
我正在考虑有一个对象数组,我可以在其中声明如下内容:
const newValues = [{property: 'Year', newValue: 2019}, {property: 'Goal', newValue: 50000}]
然后用它来比较使用filter或reduce对嵌套对象数组的迭代?有什么想法或建议吗?
【问题讨论】:
标签: javascript arrays object recursion data-manipulation