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

212
Visualizações
Attempting to use a JavaScript nested loop on specific key value pairs in an object

I am attempting to use the cocktailDB API to build a basic cocktail app and I'm looping through the results however I need to pull the ingredients out of the object but they are separate key value pairs (strIngredient 1-15). I've tried using a for in loop and can see all the properties of the object in the console but can't figure out how to loop through the ingredients to pull them out to render on the page.

var results = response.drinks;

        for (var i = 0; i < results.length; i++) {
            console.log(results[i]);
            
            var eachDrink = results;
             
            for (var prop in eachDrink[i]) {
                console.log(eachDrink[i] [prop]);
            }            

        } 

drink object screen clip

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use this code:

     let myArray = [
     { type: "Fruit", 
       color: 'blue',
       ingredient1: "salt",
       ingredient2: "water" 
     }];

    for(i=1; i <= 15; i++){
      console.log('ingreditent' + i, myArray[0]['ingredient'+i]);
    }

Or you can use this technique:

const array = { a3: 3, a2: 1, a1: 1, b: 2, c: 3 };
const keyWord = "a"; 
const filtered = Object.keys(array).filter(v => v.includes(keyWord))
result -> ["a3", "a2", "a1"];
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