如果数组已经有一些数据本机响应,如何将数据推入数组数组中?

时间:2019-02-05 04:51:00

标签: arrays react-native push state

我是新来的本地人。我在将数据推入数组时遇到问题。我有一个数组位置,在这个数组中,我有一个名为Scoresheets的子数组。在评分表数组中,我有一些数据。当我尝试将数据推入特定索引的评分表数组中时,它总是将数据推入每个位置的评分表中。

我试图将数据推入数组:

this.state.locations[1].scoreSheets[0].categories.push({
   subscore:0
});

locations: Array(34)
0: {location_id: "23", name: "Salad bar", scoreSheets: Array(2), foodInspections: Array(0), refrigerations: Array(0)}
1: {location_id: "24", name: "Grill Station ", scoreSheets: Array(2), foodInspections: Array(0), refrigerations: Array(0)}
2: {location_id: "25", name: "Entrée station", scoreSheets: Array(2), foodInspections: Array(0), refrigerations: Array(0)}
3: {location_id: "26", name: "Action station", scoreSheets: Array(2), foodInspections: Array(0), refrigerations: Array(0)}
4: {location_id: "27", name: "Sushi Station", scoreSheets: Array(2), foodInspections: Array(0), refrigerations: Array(0)}
5: {location_id: "28", name: "Soup station ", scoreSheets: Array(2), foodInspections: Array(0), refrigerations: Array(0)}
6: {location_id: "29", name: "Specialty station ", scoreSheets: Array(2), foodInspections: Array(0), refrigerations: Array(0)}

每当试图推入预期结果时,都是先将数据推入locations数组的第一个索引,然后推入scoresheet数组的第0个索引,但是它将数据推入每个位置。

1 个答案:

答案 0 :(得分:0)

您的scoreSheets对象可能正在引用相同的类别对象。每个scoreSheets对象都必须是一个不同的对象。为每个scoreSheets对象分配一个新的类别数组。示例:

var newScoreSheets = [...categories] 

分配新的类别数组,而不要引用同一对象