简体   繁体   English

javascript object 赋值一个数组

[英]javascript object Assignment an array

i have an object and with specific key a assign an array我有一个 object 并使用特定的键分配一个数组

denemeobject['items'] = Object.values(
                  JSON.parse(results.rows.item(index).BarcodeArray),
                );

problem is it looks like this:问题是它看起来像这样:

"items": [
        [
          {
            "barcode": "1245124125412",
            "qty": 3
          },
          {
            "barcode": "1254123151231",
            "qty": 1
          },
          {
            "barcode": "2352341241241",
            "qty": 1
          },
          {
            "barcode": "1241251241254",
            "qty": 1
          }
        ]
      ]

when i get data it comes to me in array.当我得到数据时,它以数组的形式出现。 but assign to in object it caused it array in array.但是分配给 object 它导致它数组中的数组。 is that normal or there is something off是正常的还是有问题

should be look like this:应该是这样的:

[
    {
      "barcode": "1245124125412",
      "qty": 3
    },
    {
      "barcode": "1254123151231",
      "qty": 1
    },
    {
      "barcode": "2352341241241",
      "qty": 1
    },
    {
      "barcode": "1241251241254",
      "qty": 1
    }
  ]

how am i supposed to do that?我该怎么做? any ideas?有任何想法吗?

You don't need Object.values , just dereference the BarcodeArray property.您不需要Object.values ,只需取消引用BarcodeArray属性即可。 You then have a plain array of objects containing barcode and qty properties, as you suggested you required.然后,根据您的建议,您将拥有一个包含barcodeqty属性的普通对象数组。

 const data = `{"BarcodeArray":[{"barcode":"1245124125412","qty":3},{"barcode":"1254123151231","qty":1},{"barcode":"2352341241241","qty":1},{"barcode":"1241251241254","qty":1}]}` console.log(JSON.parse(data).BarcodeArray)

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

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