Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

131
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

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

about 4 years ago · Juan Pablo Isaza Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda