[英]Compare two arrays and return the corresponding value
I have 2 arrays of objects:我有 2 个 arrays 对象:
const a = [ { name: 'Jack', id: '2' }, { name: 'Jill', id: '25' }, { name: 'Bill', id: '288' } ] const b = [ { title: 'alfa', a_id: '2' }, { title: 'beta', a_id: '25' } ] const test = b.map(i => { a.filter(u => { console.log(u.id === i.a_id) return Object.values(u).includes(i) }) }) console.log(test)
I have to loop trough b
, and to check if the a_id
corresponds with id from a
, to return the name from a
that check the condition a_id ==id
.我必须遍历
b
,并检查a_id
是否与 a 中的 id 对应,从a
返回名称a
检查条件a_id ==id
。 How to do this?这个怎么做?
at the final i have to get 2 names: Jill and Jack在决赛中我必须得到两个名字:吉尔和杰克
May not be the most optimal solution.可能不是最优化的解决方案。 https://jsfiddle.net/5tzqj6ek/
https://jsfiddle.net/5tzqj6ek/
const a = [ { name: 'Jack', id: '2' }, { name: 'Jill', id: '25' }, { name: 'Bill', id: '288' } ] const b = [ { title: 'alfa', a_id: '2' }, { title: 'beta', a_id: '25' } ] var matchname = []; b.forEach(objb => { a.forEach(obja => { if (objb.a_id === obja.id) { matchname.push(obja.name); } }) }) console.log(matchname);
Maybe that:也许是这样:
const a = [{ name: 'Jack', id: '2' }, { name: 'Jill', id: '25' }, { name: 'Bill', id: '288' } ] const b = [{ title: 'alfa', a_id: '2' }, { title: 'beta', a_id: '25' } ] const test = b.map(i => { a.map(j => { console.log(j.id === i.a_id? `${j.id} equals ${i.a_id}`: `not equals`) }) })
You can use forEach
and find
您可以使用
forEach
并find
const a = [{name: 'Jack',id: '2'},{name: 'Jill',id: '25'},{name: 'Bill',id: '288'}] const b = [{title: 'alfa',a_id: '2'},{title: 'beta',a_id: '25'}] b.forEach(i => { let found = a.find(u => u.id === i.a_id) if (found) { console.log(found.name) } })
Assuming your id
s are unique in your a
object, you can make a Map
from these ids as keys which points to their name value.假设您的
id
在您a
object 中是唯一的,您可以从这些 id 中创建一个Map
作为指向其名称值的键。 Then you can .map()
your b
array to get the value from each a_id
by looking it up in the Map
using .get()
:然后,您可以
.map()
您的b
数组,通过使用.get()
在Map
中查找每个a_id
来获取值:
const a = [{ name: 'Jack', id: '2' }, { name: 'Jill', id: '25' }, { name: 'Bill', id: '288' } ]; const b = [{ title: 'alfa', a_id: '2' }, { title: 'beta', a_id: '25' } ]; const idMap = new Map(a.map(({name, id}) => [id, name])); const res = b.map(({a_id}) => idMap.get(a_id)); console.log(res);
If your ids aren't unique, then you can use .reduce()
to map to a Map
of id: array
pairs, where the array
portion is the names for each id.如果您的 id 不是唯一的,那么您可以使用 .reduce
.reduce()
到 map 到id: array
对的Map
,其中array
部分是每个 id 的名称。 Then you can use .flatMap()
on the array to map each array to one larger array:然后你可以在数组上使用
.flatMap()
到 map 每个数组到一个更大的数组:
const a = [{ name: 'Jack', id: '2' }, { name: 'Jill', id: '25' }, { name: 'Bill', id: '288' } ]; const b = [{ title: 'alfa', a_id: '2' }, { title: 'beta', a_id: '25' } ]; const idMap = a.reduce((map, {id, name}) => map.set(id, (map.get(id) || []).concat(name)), new Map); const res = b.flatMap(({a_id}) => idMap.get(a_id)); console.log(res);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.