簡體   English   中英

為什么元素沒有被推入陣列?

[英]Why are elements not getting pushed into the array?

我在react-native應用程序中有以下代碼段。

responseJson是我從API調用中得到的。

下面代碼中的問題是我推入resultJson的元素在map函數調用之外不存在,即resultJson只保留為空,即使在map調用resultJson會更新。 quaterly是一個數組,但它的值在map調用后保留。 我為responseJson添加了靜態數據。

下面的代碼有什么問題?

  let responseJson = { "Q1-2018": [ { "total_rows": 742, "avg_price": "2776280.6469", "avg_sqft_price": "1592.5728", "yr": 2018, "evidence": "ACT" }, { "total_rows": 133, "avg_price": "1381205.4436", "avg_sqft_price": "974.9925", "yr": 2018, "evidence": "MOR" }, { "total_rows": 147, "avg_price": "1730154.9524", "avg_sqft_price": "1472.4966", "yr": 2018, "evidence": "MOU" }, { "total_rows": 580, "avg_price": "1797097.3069", "avg_sqft_price": "2043.7190", "yr": 2018, "evidence": "TRA" }, { "total_rows": 61, "avg_price": "4075409.8361", "avg_sqft_price": "1415.8033", "yr": 2018, "evidence": "VAL" } ], "Q2-2018": [ { "total_rows": 1090, "avg_price": "2585032.2220", "avg_sqft_price": "1578.9303", "yr": 2018, "evidence": "ACT" }, { "total_rows": 199, "avg_price": "1426902.3769", "avg_sqft_price": "1060.0000", "yr": 2018, "evidence": "MOR" }, { "total_rows": 84, "avg_price": "1900348.8214", "avg_sqft_price": "1332.6071", "yr": 2018, "evidence": "MOU" }, { "total_rows": 638, "avg_price": "1810675.6536", "avg_sqft_price": "1681.9812", "yr": 2018, "evidence": "TRA" }, { "total_rows": 60, "avg_price": "2908750.0000", "avg_sqft_price": "1458.7667", "yr": 2018, "evidence": "VAL" } ], "Q3-2018": [ { "total_rows": 971, "avg_price": "2562323.0505", "avg_sqft_price": "1569.8218", "yr": 2018, "evidence": "ACT" }, { "total_rows": 192, "avg_price": "1574309.7813", "avg_sqft_price": "1175.7865", "yr": 2018, "evidence": "MOR" }, { "total_rows": 56, "avg_price": "1807243.6429", "avg_sqft_price": "1275.6607", "yr": 2018, "evidence": "MOU" }, { "total_rows": 371, "avg_price": "1629974.8113", "avg_sqft_price": "1389.6577", "yr": 2018, "evidence": "TRA" }, { "total_rows": 65, "avg_price": "1922692.3077", "avg_sqft_price": "1341.2615", "yr": 2018, "evidence": "VAL" } ], "Q4-2018": [ { "total_rows": 1426, "avg_price": "2541386.3191", "avg_sqft_price": "1527.2468", "yr": 2018, "evidence": "ACT" }, { "total_rows": 163, "avg_price": "1440970.0491", "avg_sqft_price": "952.3129", "yr": 2018, "evidence": "MOR" }, { "total_rows": 91, "avg_price": "1571781.8352", "avg_sqft_price": "1244.3077", "yr": 2018, "evidence": "MOU" }, { "total_rows": 429, "avg_price": "1626291.2727", "avg_sqft_price": "1434.8834", "yr": 2018, "evidence": "TRA" }, { "total_rows": 61, "avg_price": "1797540.9836", "avg_sqft_price": "1251.2623", "yr": 2018, "evidence": "VAL" } ], "Q1-2019": [ { "total_rows": 1391, "avg_price": "2755570.1855", "avg_sqft_price": "1457.6204", "yr": 2019, "evidence": "ACT" }, { "total_rows": 126, "avg_price": "1005182.1111", "avg_sqft_price": "812.4762", "yr": 2019, "evidence": "MOR" }, { "total_rows": 18, "avg_price": "1680666.6667", "avg_sqft_price": "1287.3333", "yr": 2019, "evidence": "MOU" }, { "total_rows": 362, "avg_price": "1769586.9586", "avg_sqft_price": "1506.7099", "yr": 2019, "evidence": "TRA" }, { "total_rows": 40, "avg_price": "1974375.0000", "avg_sqft_price": "1292.5500", "yr": 2019, "evidence": "VAL" }]}; let resultJson = []; let avgJson = []; let quarterly = []; Object.keys(responseJson).map((value, key) => { quarterly.push(value); responseJson[value].map((value, key) => { const evidence = value.evidence; if (typeof resultJson[evidence] === "undefined") { resultJson[evidence] = []; } resultJson[evidence].push(value); console.log( "resultJson[evidence]", resultJson[evidence].length, resultJson[evidence] ); }); }); 

您正嘗試從數組中訪問鍵,就像它是一個對象一樣。 不知何故,Javascript允許它,並且實際上存儲它們,所以你可以在之后執行console.log(resultJson["ACT"])並仍然獲取值。

但是將resultJson作為對象可能更有意義。

let resultJson = {}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM