我正試圖從下面的多維陣列中獲取DishId,但我無法實作它。下面是代碼的細節。
count = 0;
for (j = 0; j < OrderDishes.length; j ) {
for (k = 0; k < OrderDishes[j].length; k ) {
dishId.push(OrderDishes[k].DishId) 。
count = count 1;
if(count == OrderDishes[j].length){
console.log(dishId)。
return res.json({
success: 1.
});
}
}
OrderDishes下面是一個陣列 :
[ [ { DishId: 43。
DishName: 'Kuchvi Biryani',
辣味: 2,
UnitPrice: '6.99',
Qty: 5,
DishTotal: '34.95' }. ],
[{ DishId: 41,
DishName: 'Dum Biryani',
辣味: 2,
UnitPrice: '6.99',
Qty: 5,
DishTotal: '34.95' },
{ DishId: 42,
DishName: 'Tysoon Biryani',
辣味: 2,
UnitPrice: '6.99',
Qty: 5,
DishTotal: '34.95' } ] ]
當我運行時,我得到了43和41,而我正在尋找43,41和42
uj5u.com熱心網友回復:你只得到了43和41,因為有計數條件。你的外層陣列的長度是2。菜名43是一個陣列,菜名41和42在第二個陣列中。一旦計數達到2,它就會停止。
嘗試將你的count 和if條件移出內回圈。
。const OrderDishes = [
[{
DishId: 43,
DishName: 'Kuchvi Biryani',
辣味: 2,
UnitPrice: '6.99',
Qty: 5,
DishTotal: '34.95'。
}],
[{
DishId: 41,
DishName: 'Dum Biryani',
辣味: 2,
UnitPrice: '6.99',
Qty: 5,
DishTotal: '34.95'.
},
{
DishId: 42,
DishName: 'Tysoon Biryani',
辣味: 2,
UnitPrice: '6.99',
Qty: 5,
DishTotal: '34.95'.
}
]
]
count = 0;
let dishId = [] 。
for (j = 0; j < OrderDishes.length; j ) {
for (k = 0; k < OrderDishes[j].length; k ) {
dishId.push(OrderDishes[j][k].DishId) 。
}
count ;
if (count === OrderDishes.length) {
console.log('dishID: ', dishId)。
}
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
嘗試 dishId.push(OrderDishes[j][k].DishId); , 你使用的是多維陣列,所以,你需要通過col id和row id來訪問一個單元。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/327645.html
標籤:
上一篇:process.mainModule在nodejs中被棄用了。
下一篇:谷歌云運行沒有按照預期進行擴展
