i have arrays like this:
[
0: {barcode: '4124121241241', qty: 1}
1: {barcode: '1234124135134', qty: 1}
2: {barcode: '3573424625234', qty: 1}
3: {barcode: '2346468457354', qty: 1}
4: {barcode: '5684452043432', qty: 1}
5: {barcode: '1241263475686', qty: 5}
6: {barcode: '1231241231423', qty: 15}
]
and i'm pushing those like this:
shelfArray.push({
[rafName]:JSON.parse(results.rows.item(index).BarcodeArray)
});
this is my output:
0:
Q1:
BarcodeArray: [{…}]
1:
Q2:
BarcodeArray: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
2: {Q3: null}
but i want this like :
Q1:
BarcodeArray: [{…}]
Q2:
BarcodeArray: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
2: {Q3: null}
how can do that ?
CodePudding user response:
Rather than creating shelfArray
as an array []
, create it as an empty object
const shelfArray = {}; // rather than `= [];`
and then add keys to that object as you loop instead of pushing by using bracket notation:
shelfArray[rafName] = JSON.parse(results.rows.item(index).BarcodeArray);