需要替换对象值,下面是我需要补充对象值的代码。但是这里我不希望整个数组长度,直接想要替换Mon
值。 weekday
是动态/已知的。
array = [{
Mon: "0",
Tue: "1",
Wed: "0"
},
{
Mon: "0",
Tue: "1",
Wed: "0"
}
]
var weekday = "Mon";
array[0].weekday = "1"
答案 0 :(得分:2)
您必须使用[]
而不是点符号
array = [{
Mon: "0",
Tue: "1",
Wed: "0"
},
{
Mon: "0",
Tue: "1",
Wed: "0"
}
]
var weekday = "Mon";
array[0][weekday] = "1"
console.log(array);