Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda