2

Need to replace the object value, below is the code where I need to replece object value. But here I don't want entire array length, directly want to replace Mon value. weekdayis dynamic/ known.

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


var weekday = "Mon";
array[0].weekday = "1"
Arun Kumar M
  • 1,633
  • 1
  • 15
  • 26

1 Answers1

2

You have to wrap with [] instead of dot notation

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


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

console.log(array);
Eddie
  • 26,593
  • 6
  • 36
  • 58