简体   繁体   English

比较两个 arrays 并返回对应值

[英]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您可以使用forEachfind

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM