我想知道如何从javascript中的数组更改嵌套数组中的值。
我有一个 obj
和 arr
,其中我需要用 javascript 中的 arr 值替换 month
值,
即“12”应该是“Dec”
var arr = ["Jan", "Feb","Mar", "Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
var obj =[{
"month": "12",
"year": "2013"
},
{
"month": "10",
"year": "2012"
}]
预期输出:
[{
"month": "Dec",
"year": "2013"
},
{
"month": "Oct",
"year": "2012"
}]
最佳答案
您可以使用.map()
获得所需的输出:
const months = ["Jan", "Feb","Mar", "Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
const data =[{"month": "12", "year": "2013"}, { "month": "10", "year": "2012"}];
const result = data.map(
o => ({...o, month: months[o.month - 1]})
);
console.log(result);
关于javascript - 从数组javascript更改对象数组中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59712942/