首页 > 解决方案 > 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++;
}

标签: javascript

解决方案


它非常可压缩

这不能回答你的问题(你的问题是什么),但更容易阅读

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


推荐阅读