需要替换对象值

时间:2018-04-02 14:49:25

标签: javascript jquery

需要替换对象值,下面是我需要补充对象值的代码。但是这里我不希望整个数组长度,直接想要替换Mon值。 weekday是动态/已知的。

array = [{
        Mon: "0",
        Tue: "1",
        Wed: "0"
    },
     {
        Mon: "0",
        Tue: "1",
        Wed: "0"
    }
]


var weekday = "Mon";
array[0].weekday = "1"

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);