Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

176
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda