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

react native creating object with arrays

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:

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

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 ?

>Solution :

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);
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