简体   繁体   English

反应本机创建 object 与 arrays

[英]react native creating object with arrays

i have arrays like this:我有这样的 arrays:

[
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:这是我的 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?怎么能那样呢?

Rather than creating shelfArray as an array [] , create it as an empty object与其将shelfArray创建为数组[] ,不如将其创建为空 object

const shelfArray = {}; // rather than `= [];`

and then add keys to that object as you loop instead of pushing by using bracket notation :然后在循环时向 object 添加键,而不是使用括号表示法推送:

shelfArray[rafName] = JSON.parse(results.rows.item(index).BarcodeArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM