簡體   English   中英

javascript中的多維數組

[英]Multi dimensional Array in javascript

最近我從我的同事那里收到了一份 javascript 代碼的副本,我對此感到非常困惑。 請給我建議或任何參考。

 for (p = 0; p < countITEM; p++) { arrayCATEGORY_item[arrayITEM[p].category].image1[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].image1; arrayCATEGORY_item[arrayITEM[p].category].title[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].title; arrayCATEGORY_item[arrayITEM[p].category].description[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].description; arrayCATEGORY_item[arrayITEM[p].category].unitprice[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].unitprice; arrayCATEGORY_item[arrayITEM[p].category].avai[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].avai; arrayCATEGORY_item[arrayITEM[p].category].option1[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].option1; arrayCATEGORY_item[arrayITEM[p].category].option1_unitprice[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].option1_unitprice; arrayCATEGORY_item[arrayITEM[p].category].option2[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].option2; arrayCATEGORY_item[arrayITEM[p].category].option2_unitprice[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].option2_unitprice; arrayCATEGORY_item[arrayITEM[p].category].option3[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].option3; arrayCATEGORY_item[arrayITEM[p].category].option3_unitprice[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].option3_unitprice; arrayCATEGORY_item[arrayITEM[p].category].option4[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].option4; arrayCATEGORY_item[arrayITEM[p].category].option4_unitprice[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].option4_unitprice; arrayCATEGORY_item[arrayITEM[p].category].option5[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].option5; arrayCATEGORY_item[arrayITEM[p].category].option5_unitprice[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].option5_unitprice; arrayCATEGORY_item[arrayITEM[p].category].addon1[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].addon1; arrayCATEGORY_item[arrayITEM[p].category].addon1_unitprice[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].addon1_unitprice; arrayCATEGORY_item[arrayITEM[p].category].addon2[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].addon2; arrayCATEGORY_item[arrayITEM[p].category].addon2_unitprice[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].addon2_unitprice; arrayCATEGORY_item[arrayITEM[p].category].addon3[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].addon3; arrayCATEGORY_item[arrayITEM[p].category].addon3_unitprice[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].addon3_unitprice; arrayCATEGORY_item[arrayITEM[p].category].addon4[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].addon4; arrayCATEGORY_item[arrayITEM[p].category].addon4_unitprice[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].addon4_unitprice; arrayCATEGORY_item[arrayITEM[p].category].addon5[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].addon5; arrayCATEGORY_item[arrayITEM[p].category].addon5_unitprice[arrayCATEGORY_item[arrayITEM[p].category].ptrITEM] = arrayITEM[p].addon5_unitprice; arrayCATEGORY_item[arrayITEM[p].category].countITEM++; arrayCATEGORY_item[arrayITEM[p].category].ptrITEM++; }

它非常可壓縮

這不能回答你的問題(你的問題是什么),但更容易閱讀

 countITEM.forEach((item, p) => { const aItem = arrayITEM[p]; const cat = aItem.category; const ptrITEM = arrayCATEGORY_item[cat].ptrITEM arrayCATEGORY_item[cat].image1[ptrITEM] = aItem.image1; arrayCATEGORY_item[cat].title[ptrITEM] = aItem.title; arrayCATEGORY_item[cat].description[ptrITEM] = aItem.description; arrayCATEGORY_item[cat].unitprice[ptrITEM] = aItem.unitprice; arrayCATEGORY_item[cat].avai[ptrITEM] = aItem.avai; for (let i = 1; i <= 5; i++) { arrayCATEGORY_item[cat]["option" + i][ptrITEM] = aItem["option" + i]; arrayCATEGORY_item[cat]["option" + i + "_unitprice"][ptrITEM] = aItem["option" + i + "_unitprice"]; arrayCATEGORY_item[cat]["addon" + i][ptrITEM] = aItem["addon" + i]; arrayCATEGORY_item[cat]["addon" + i + "_unitprice"][ptrITEM] = aItem["addon" + i + "_unitprice"]; arrayCATEGORY_item[cat].countITEM++; ptrITEM++; } })

暫無
暫無

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

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