1

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]}
0

3 Answers 3

1

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});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much but it does not work. I don't know why.
@Darwin Quiñones Could you provide more infos or code so that we could reproduce, what isnt working?
1
let cantidad
toolList[0]["cantidad"] = cantidad

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.