繁体   English   中英

如何在javascript中创建一个新的索引数组?

[英]How to create a new index array in javascript?

我有一个数组 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']}

如何创建另一个索引来添加一个名为 cantidad 的新变量? 例如,要分配给 cantidad 索引的 cantidad 变量。

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

使用 JavaScript 方法push()将具有索引'cantidad'和变量cantidad值的新项目添加到关联数组:

let cantidad = 'test';

toolList.push({'cantidad': cantidad});
let cantidad
toolList[0]["cantidad"] = cantidad

当变量名与对象的键名匹配时,您可以使用 ES6 语法,如下所示:

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

push 方法的 MDN 文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM