Home > OS >  javascript object Assignment an array
javascript object Assignment an array

Time:04-29

i have an object and with specific key a assign an array

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. 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?

CodePudding user response:

You don't need Object.values, just dereference the BarcodeArray property. You then have a plain array of objects containing barcode and qty properties, as you suggested you required.

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)

  • Related