jeudi 26 décembre 2019

How to populate an array of objects dynamically?

let products = [
  {
    name: "a",
    inventory: 1
  },
  {
    name: "b",
    inventory: 2
  },
  {
    name: "c",
    inventory: 3
  }
];

How can I go about creating these objects in an array like this dynamically ? Here is what I have been trying to do.

choicesHolder = [{name: '', inventory: ''}];
for(i; element.length<0; i--){
 
choicesHolder.push({"name": element.value, "inventory": element.selected});

element holds the data from the JSON.




Aucun commentaire:

Enregistrer un commentaire