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

123
Views
How to iterate through two arrays and return key values to different object
const phoneBook = {};
const names = ['Mira', 'Royce', 'Kathie'];
const numbers = ['3234958675', '9164059384', '4154958675']

function populate(names, numbers) {
  for(i = 0; i < populate.length; i++) {
    phoneBook[names[i]] = numbers[i];
}
  
}

How can I get my for loop to iterate through both arrays simultaneously and return names as a key and numbers as the values?

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

0

You mostly have the right idea.

Two suggestions:

  1. you want to use numbers.length or names.length as the number of iterations
  2. you probably don't want to mutate a global object

A working example would look something like this:

const names = ['Mira', 'Royce', 'Kathie'];
const numbers = ['3234958675', '9164059384', '4154958675'];

function populate(names, numbers) {
    const phoneBook = {};

    for(i = 0; i < names.length; i++) {
        phoneBook[names[i]] = numbers[i];
    }

    return phoneBook;
}

const phoneBook = populate(names, numbers);
about 4 years ago · Juan Pablo Isaza Report

0

More concisely, zip the arrays together, then pass that to Object.fromEntries(0

function makePhoneBook(names, numbers) {
  const entries = names.map((name, i) => [name, numbers[i]]);
  return Object.fromEntries(entries);
}

const names = ['Mira', 'Royce', 'Kathie'];
const numbers = ['3234958675', '9164059384', '4154958675']
const phoneBook = makePhoneBook(names, numbers);
console.log(phoneBook);

about 4 years ago · Juan Pablo Isaza Report

0

Just for the note, lodash if you don't mind:

const names = ['Mira', 'Royce', 'Kathie'];
const numbers = ['3234958675', '9164059384', '4154958675'];

const phoneBook = _.zipObject(names, numbers);

console.log(phoneBook);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

And, of course, one-liner:

const names = ['Mira', 'Royce', 'Kathie'];
const numbers = ['3234958675', '9164059384', '4154958675'];

const phoneBook = names.reduce((acc, name, i) => (acc[name] = numbers[i], acc), {});

console.log(phoneBook);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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!