Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

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

about 4 years ago · Juan Pablo Isaza Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda