简体   繁体   English

javascript中的多维数组

[英]Multi dimensional Array in javascript

Recently I received a copy of javascript code from my colleague, I totally confused with this.最近我从我的同事那里收到了一份 javascript 代码的副本,我对此感到非常困惑。 Please give me advice or any reference.请给我建议或任何参考。

 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++; }

It is very compressible它非常可压缩

That does not answer your question (What IS your question) but makes it easier to read这不能回答你的问题(你的问题是什么),但更容易阅读

 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