Home > Software design >  how to update argument's value of list in JavaScript
how to update argument's value of list in JavaScript

Time:09-12

hi i have a list with three empty object like :

TimePickerList =[{},{},{}];

and for Example i want to add 1 and 2 to first argument of this list like :

TimePickerList[0].push("1") // i know it's wrong way and i cant use push like this .
TimePickerList[0].push("2")

and i want to get this :

TimePickerList =[{"1","2"},{},{}];

is there any way to do this? i mean can i update argumant value?

CodePudding user response:

You can update values stored in arrays like such:


TimePickerList = [{}, {}, {}];

TimePickerList[0].a = 1;

console.log(TimePickerList)
   // [{ a: 1 }, {}, {}]

The reason it wasn't working for you is because you were trying to push a value to an object. If you want to push values then push to an array, therefore using square brackets [] instead of curly braces {}.

CodePudding user response:

Not really. I think you looking for an array with arrays.

const t = [[],[]];

t[0].push(1);
t[0].push(2)
console.log(t)

  • Related