Home > Back-end >  How to get the same value from 2 different arrays
How to get the same value from 2 different arrays

Time:05-03

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

CodePudding user response:

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]));

CodePudding user response:

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

CodePudding user response:

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));

CodePudding user response:

Safe navigated & case-insensitive solution (Works if e.g.id is missing from zoo and if the animals are not named in the same case-sensitivity):

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 selectedLowerCased = selectedAnimals.map(sa => sa.toLowerCase())

const animalsWithIds = zoo.filter(animal => {
    const name = animal && animal.name
    const id = animal && animal.id
    return name && id && selectedLowerCased.includes(name.toLowerCase())
})

CodePudding user response:

You can do:

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 zooHash = zoo.reduce((a, { id, name }) => (a[name] = id, a), {})
const result = selectedAnimals.map(name => zooHash[name])

console.log(result)

  • Related