如何从两个不同的数组中获取相同的值
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'}]
嗨!有两个不同的数组,我想比较两个数组并从动物园中找到所选动物的 ID。如何获取 ID 数组?另外,id 必须是一个字符串。谢谢
要获取每个id,你可以const idList = zoo.map(item => item.id)
创建一个 look-up table 来给出每个名称的 ID,然后使用它来获取每个选定动物的 ID。
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]));
这是天真的解决方案:
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));
但实际上,您应该更好地构建数据,这样就不必使用嵌套循环:
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));
安全导航和 case-insensitive 解决方案(如果 zoo
中缺少 id
并且动物未在同一 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())
})
你可以这样做:
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)
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'}]
嗨!有两个不同的数组,我想比较两个数组并从动物园中找到所选动物的 ID。如何获取 ID 数组?另外,id 必须是一个字符串。谢谢
要获取每个id,你可以const idList = zoo.map(item => item.id)
创建一个 look-up table 来给出每个名称的 ID,然后使用它来获取每个选定动物的 ID。
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]));
这是天真的解决方案:
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));
但实际上,您应该更好地构建数据,这样就不必使用嵌套循环:
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));
安全导航和 case-insensitive 解决方案(如果 zoo
中缺少 id
并且动物未在同一 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())
})
你可以这样做:
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)