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

211
Views
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 answers
Answer question

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 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!