Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
Item cant be accessed from multidimensional array

I am trying to fetch DishId from below multidimensional array but I am not able to achieve it. Below is the code details.

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 is below Array :

[ [ { DishId: 43,
      DishName: 'Kuchvi Biryani',
      Spicy: 2,
      UnitPrice: '6.99',
      Qty: 5,
      DishTotal: '34.95' } ],
      
  [ { DishId: 41,
      DishName: 'Dum Biryani',
      Spicy: 2,
      UnitPrice: '6.99',
      Qty: 5,
      DishTotal: '34.95' },
    { DishId: 42,
      DishName: 'Tysoon Biryani',
      Spicy: 2,
      UnitPrice: '6.99',
      Qty: 5,
      DishTotal: '34.95' } ] ]

When i run, i am getting 43 and 41 , instead i was looking for 43, 41 and 42

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You are getting only 43 and 41 because of the count condition. The length of your outer array is 2. dishId 43 is one array and dish 41 and 42 are in a second array. As soon as count reaches 2, it stops.

Try to move your count++ and if condition out of the inner loop.

const OrderDishes = [
  [{
    DishId: 43,
    DishName: 'Kuchvi Biryani',
    Spicy: 2,
    UnitPrice: '6.99',
    Qty: 5,
    DishTotal: '34.95'
  }],

  [{
      DishId: 41,
      DishName: 'Dum Biryani',
      Spicy: 2,
      UnitPrice: '6.99',
      Qty: 5,
      DishTotal: '34.95'
    },
    {
      DishId: 42,
      DishName: 'Tysoon Biryani',
      Spicy: 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);

  }
}

about 4 years ago · Juan Pablo Isaza Report

0

try dishId.push(OrderDishes[j][k].DishId); , you are using multidimensional array , so , you need to get access to a cell by col id and row id.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!