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

203
Views
Is there a way to iterate over 2 arrays with the same index

I have 2 arrays ingredients and measures in my margaritas object and I'm trying to render a list of ingredients to the dom where ingredients[0] is an ingredient and measures[0] is the measure of the ingredient, and so on. I cannot figure out how to make this work and I'm wondering if maybe i need to restructure my code or if i'm making a mistake somewhere.

The code is here at: https://jsfiddle.net/morjman/m4ucy0t7/3/

When I tried to do it I created a renderIngredients function which takes 2 arrays and a parent element but I think that's wrong as well

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

0

why not use a regular good old array iteration? Assuming ingredients and measures have the same array elements, you can do sth like this,

    for(let i = 0; i< measures.length; i++){
        //Do stuff on ingredients[i];
        //Do some other stuff with measures[i];

    }

Make sure they have equal number of items or else you'll end up pointing to null.

about 4 years ago · Juan Pablo Isaza Report

0

Like someone else mentioned, you can use a for loop:

  let numIngredients = margarita.ingredients.length;
  let strIngredients = ''; 
  
  for(let i = 0; i < numIngredients; i++) {
 
    let ing =  margarita.ingredients[i] || '';
    let meas = margarita.measures[i] || '';
  
    strIngredients += '<li>' +  ing  + " " + meas + '</li>'
 }
 
  const html = `
      <div class="margarita-details">
          <h2><span>${margarita.id}</span> ${margarita.name}</h2>
          <ul>${strIngredients}</ul>
          <img src="${margarita.img}" alt="${margarita.imgAlt}" />
          <p class="instructions">${margarita.instructions}</p>
          <p class="">${margarita.isAlcoholic ? "Contains Alcohol!!" : ""}</p>
        </div>
      `;
about 4 years ago · Juan Pablo Isaza Report

0

There are two ways you can do this. But for both methods, arrays should be in equal length.

First method is the most classic way.

for(let i = 0; i< measures.length; i++){
    ingred = ingredients[i]
    measure = measures[i]
    // do something
}

The second way to do it is zip both arrays first and then iterate.

var zipped = ingredients.map(function(e, i) {
  return [e, measures[i]];
});
// zipped = [[ingred1,mes1],[ingred2,mes2],[ingred3,mes3],.....]
// Then iterate through the zipped array

And there are several other methods to zip arrays in js. refer this

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!