Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

javascript object Assignment an array

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading