'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
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 1:[1]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dave Meehan |
