javascript - 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++;
}
})
推荐阅读
- php - AWS:PHP 代码是可见的,而不是在虚拟主机中执行它
- javascript - 我在将 hashMap String 发送到服务器 php 时遇到问题,而不是在 volly 响应 android 中返回 JsonArray
- java - java 如何对列表中最早的时间戳进行排序?
- c - 我写了 RegCreateKeyEx () 但它失败了。它的原因是什么?
- javascript - Node.js 请求使用“请求”模块,但不适用于“axios”
- hibernate - 我需要将哪些 c3p0 属性用于休眠池?
- php - 为什么在我可以将代码放入文件时使用特征
- python - _csv.Error:在将 csv 转换为 excel 时,python 中的行包含 NULL 字节
- node.js - Google 助理的网络抓取响应错误
- android - 查询子集合 firestore 中的文档