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

129
Views
How to get the same value from 2 different arrays
const selectedAnimals = ['lion','tiger','elephant','deer','bird','turtle']
const zoo = [{id: '1', name:'lion'},{id: '2', name:'panda'},{id: '3', name:'tiger'},{id: '4', name:'rabbit'},{id: '5', name:'bear'},{id: '6', name:'elephant'},{id: '7', name:'deer'},{id: '8', name:'bird'},{id: '9', name:'turtle'}]

Hi! There are two different arrays and I want to compare two arrays and find the id of selected animals from the zoo. How do I get the array of ids? Also, the id has to be a string. Thank you

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

0

Make a look-up table that gives the ID for each name, then use this to get the IDs of each selected animal.

const selectedAnimals = ['lion','tiger','elephant','deer','bird','turtle'];
const zoo = [{id: '1', name:'lion'},{id: '2', name:'panda'},{id: '3', name:'tiger'},{id: '4', name:'rabbit'},{id: '5', name:'bear'},{id: '6', name:'elephant'},{id: '7', name:'deer'},{id: '8', name:'bird'},{id: '9', name:'turtle'}];
const idByName = Object.fromEntries(zoo.map(item => [item.name, item.id]));
console.log(selectedAnimals.map(name => idByName[name]));

about 4 years ago · Juan Pablo Isaza Report

0

To get each id, you can do const idList = zoo.map(item => item.id)

about 4 years ago · Juan Pablo Isaza Report

0

Here is the naive solution:

const selectedAnimals = ['lion', 'tiger', 'elephant', 'deer', 'bird', 'turtle'];

const zoo = [
    { id: '1', name: 'lion' },
    { id: '2', name: 'panda' },
    { id: '3', name: 'tiger' },
    { id: '4', name: 'rabbit' },
    { id: '5', name: 'bear' },
    { id: '6', name: 'elephant' },
    { id: '7', name: 'deer' },
    { id: '8', name: 'bird' },
    { id: '9', name: 'turtle' },
];

const getSelectedAnimalIds = (animals) => {
    return animals.map((animal) => zoo.find(({ name }) => name === animal).id);
};

console.log(getSelectedAnimalIds(selectedAnimals));

But in reality, you should be structuring your data better so you wouldn't have to use a nested loop:

const selectedAnimals = ['lion', 'tiger', 'elephant', 'deer', 'bird', 'turtle'];

const zoo = [
    { id: '1', name: 'lion' },
    { id: '2', name: 'panda' },
    { id: '3', name: 'tiger' },
    { id: '4', name: 'rabbit' },
    { id: '5', name: 'bear' },
    { id: '6', name: 'elephant' },
    { id: '7', name: 'deer' },
    { id: '8', name: 'bird' },
    { id: '9', name: 'turtle' },
];

// This is how your data should be structured to start with
const zooMap = Object.fromEntries(zoo.map(({ name, id }) => [name, id]));

const getSelectedAnimalIds = (animals) => {
    return animals.map((animal) => zooMap[animal]);
};

console.log(getSelectedAnimalIds(selectedAnimals));

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!