Home > Blockchain >  How to create a new index array in javascript?
How to create a new index array in javascript?

Time:11-07

I have an array toolList

toolList = [{
      tbl_gaveta_NUMERO: '3',
      tbl_gaveta_DESCRIPCION: 'GAVETA CLORO',
      tbl_herramienta_FECHA: '2021-11-02',
      tbl_herramienta_NOMBRE: 'EXACTO',
      tbl_herramienta_CODIGO: '354']}

How to create another index to add a new variable called cantidad? As an example, the cantidad variable to be assigned to cantidad index.

toolList = [{
      tbl_gaveta_NUMERO: '3',
      tbl_gaveta_DESCRIPCION: 'GAVETA CLORO',
      tbl_herramienta_FECHA: '2021-11-02',
      tbl_herramienta_NOMBRE: 'EXACTO',
      tbl_herramienta_CODIGO: '354',
      cantidad: cantidad]}

CodePudding user response:

Adding a new item with the index 'cantidad' and value of the variable cantidad to your associative array using JavaScript method push():

let cantidad = 'test';

toolList.push({'cantidad': cantidad});

CodePudding user response:

let cantidad
toolList[0]["cantidad"] = cantidad

CodePudding user response:

When the variable name matches key name of the object, you can use ES6 syntax like that:

const cantidad = 'cantidad'
toolList.push({ cantidad })

MDN docs of the push method

  • Related