How do I compare two arrays of objects to see if they have the same ids and then return another value from the matching object?

huangapple go评论98阅读模式
英文:

How do I compare two arrays of objects to see if they have the same ids and then return another value from the matching object?

问题

以下是翻译好的部分:

我想要编写一个函数用于比较`sample1`中的`id``sample2`中的`id`如果`id`匹配我需要返回`sample2`中的`dish`我尝试过在`sample1`上进行映射并在`sample2`上的循环中使用`find()`但无法使其工作
英文:

So here are two sample arrays:

const sample1 = [{id: '1', name: 'jon', quantity: '5'}, {id: '2', name: 'sue', quantity: '4'}, {id: '3', name: 'greg', quantity: '7'}];

const sample2 = [{id: '1', dish: 'cheesecake'}, {id: '2', dish: 'carrot cake'}, {id: '3', dish: 'cupcake'}];

I would like to write a function to compare the ids from sample1 with the ids of sample2. If the ids match I need to return the dish value from sample2. I tried mapping sample1 and using find() inside the loop on sample2 but could not get it to work.

答案1

得分: 0

如果您想获取所有在sample1中具有匹配id的sample2菜品,以下代码可以实现此目的。

const sample1 = [{id: '1', name: 'jon', quantity: '5'}, {id: '2', name: 'sue', quantity: '4'}, {id: '3', name: 'greg', quantity: '7'}];

const sample2 = [{id: '1', dish: 'cheesecake'}, {id: '2', dish: 'carrot cake'}, {id: '3', dish: 'cupcake'}];

// 包含所有在Sample1中具有匹配id的Sample2菜品的数组
const result = sample2
  .filter(sample2Entry => sample1.some(sample1Entry => sample1Entry.id === sample2Entry.id))
  .map(sample2Entry => sample2Entry.dish);
英文:

If you want all sample2 dishes that have a matching id in sample1, the following code does the trick.

const sample1 = [{id: '1', name: 'jon', quantity: '5'}, {id: '2', name: 'sue', quantity: '4'}, {id: '3', name: 'greg', quantity: '7'}];

const sample2 = [{id: '1', dish: 'cheesecake'}, {id: '2', dish: 'carrot cake'}, {id: '3', dish: 'cupcake'}];

// Array with all Sample2 dishes that have a matching id in Sample1
const result = sample2
  .filter(sample2Entry => sample1.some(sample1Entry => sample1Entry.id === sample2Entry.id))
  .map(sample2Entry => sample2Entry.dish);

答案2

得分: 0

如果您想根据ID将两者合并,可以像这样操作:

const sample1 = [{id: '1', name: 'jon', quantity: '5'}, {id: '2', name: 'sue', quantity: '4'}, {id: '3', name: 'greg', quantity: '7'}];

const sample2 = [{id: '1', dish: 'cheesecake'}, {id: '2', dish: 'carrot cake'}, {id: '3', dish: 'cupcake'}];

const result = sample1.map(s1item => ({
  ...s1item,
  dish: sample2.find(s2item => s2item.id === s1item.id) ? sample2.find(s2item => s2item.id === s1item.id).dish : null
}))

console.log(result)
英文:

If u want to combine the two based on the id you can do it like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const sample1 = [{id: &#39;1&#39;, name: &#39;jon&#39;, quantity: &#39;5&#39;}, {id: &#39;2&#39;, name: &#39;sue&#39;, quantity: &#39;4&#39;}, {id: &#39;3&#39;, name: &#39;greg&#39;, quantity: &#39;7&#39;}];

const sample2 = [{id: &#39;1&#39;, dish: &#39;cheesecake&#39;}, {id: &#39;2&#39;, dish: &#39;carrot cake&#39;}, {id: &#39;3&#39;, dish: &#39;cupcake&#39;}];
  
const result = sample1.map(s1item =&gt; ({
  ...s1item,
  dish: sample2.find(s2item =&gt; s2item.id === s1item.id) ? sample2.find(s2item =&gt; s2item.id === s1item.id).dish : null
}))
  
console.log(result)

<!-- end snippet -->

答案3

得分: 0

你可以使用 Set 来筛选对象并映射所需的属性。

const
    sample1 = [{ id: '1', name: 'jon', quantity: '5' }, { id: '2', name: 'sue', quantity: '4' }, { id: '3', name: 'greg', quantity: '7' }],
    sample2 = [{ id: '1', dish: 'cheesecake' }, { id: '2', dish: 'carrot cake' }, { id: '3', dish: 'cupcake' }],
    set1 = new Set(sample1.map(({ id }) => id)),
    result = sample2
        .filter(({ id }) => set1.has(id))
        .map(({ dish }) => dish);

console.log(result);
英文:

You could take a Set, filter the objects and map the wanted property.

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
sample1 = [{ id: '1', name: 'jon', quantity: '5' }, { id: '2', name: 'sue', quantity: '4' }, { id: '3', name: 'greg', quantity: '7' }],
sample2 = [{ id: '1', dish: 'cheesecake' }, { id: '2', dish: 'carrot cake' }, { id: '3', dish: 'cupcake' }],
set1 = new Set(sample1.map(({ id }) => id)),
result = sample2
.filter(({ id }) => set1.has(id))
.map(({ dish }) => dish);

console.log(result);

<!-- end snippet -->

答案4

得分: 0

你可以首先使用 .map() 方法将你的 sample1 数组映射为一个 ids 数组。然后,你可以从你的 sample2 数组构建一个查找表,将菜品与 id 键关联起来。然后,使用最初使用 .map() 创建的数组,你可以使用 .flatMap() 方法遍历该数组,以获取每个给定用户的所有菜品:

const sample1 = [{id: '1', name: 'jon', quantity: '5'}, {id: '2', name: 'sue', quantity: '4'}, {id: '3', name: 'greg', quantity: '7'}];
const sample2 = [{id: '1', dish: 'cheesecake'}, {id: '2', dish: 'carrot cake'}, {id: '3', dish: 'cupcake'}];

const sampe1map = sample1.map(({id}) => id);
const lut = sample2.reduce((acc, {id, dish}) => {
  acc[id] = [...(acc[id] || []), dish];
  return acc;
}, {});
const res = sampe1map.flatMap(x => lut[x]);

console.log(res);

如果你的 sample2 中的 id 可以保证唯一,你可以使用 .map() 方法而不是 .flatMap() 方法,并使用 new Map() 来创建查找表:

const sample1 = [{id: '1', name: 'jon', quantity: '5'}, {id: '2', name: 'sue', quantity: '4'}, {id: '3', name: 'greg', quantity: '7'}];
const sample2 = [{id: '1', dish: 'cheesecake'}, {id: '2', dish: 'carrot cake'}, {id: '3', dish: 'cupcake'}];

const sampe1map = sample1.map(({id}) => id);
const lut = new Map(sample2.map(({id, dish}) => [id, dish]));
const res = sampe1map.map(x => lut.get(x));

console.log(res);
英文:

You could firstly .map() your sample1 array to an array of ids. You can then build a look up table from your sample2 array which associates an array of dishes with an id key. Then using the array you made initially using .map(), you can .flatMap() over that array to get all your dishes for each given user:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const sample1 = [{id: &#39;1&#39;, name: &#39;jon&#39;, quantity: &#39;5&#39;}, {id: &#39;2&#39;, name: &#39;sue&#39;, quantity: &#39;4&#39;}, {id: &#39;3&#39;, name: &#39;greg&#39;, quantity: &#39;7&#39;}];
const sample2 = [{id: &#39;1&#39;, dish: &#39;cheesecake&#39;}, {id: &#39;2&#39;, dish: &#39;carrot cake&#39;}, {id: &#39;3&#39;, dish: &#39;cupcake&#39;}];

const sampe1map = sample1.map(({id}) =&gt; id);
const lut = sample2.reduce((acc, {id, dish}) =&gt; {
  acc[id] = [...(acc[id] || []), dish];
  return acc;
}, {});
const res = sampe1map.flatMap(x =&gt; lut[x]);

console.log(res);

<!-- end snippet -->

If your ids in sample2 can be unique, you can use .map() instead of .flatMap() with a new Map() instead for your look up table:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const sample1 = [{id: &#39;1&#39;, name: &#39;jon&#39;, quantity: &#39;5&#39;}, {id: &#39;2&#39;, name: &#39;sue&#39;, quantity: &#39;4&#39;}, {id: &#39;3&#39;, name: &#39;greg&#39;, quantity: &#39;7&#39;}];
const sample2 = [{id: &#39;1&#39;, dish: &#39;cheesecake&#39;}, {id: &#39;2&#39;, dish: &#39;carrot cake&#39;}, {id: &#39;3&#39;, dish: &#39;cupcake&#39;}];

const sampe1map = sample1.map(({id}) =&gt; id);
const lut = new Map(sample2.map(({id, dish}) =&gt; [id, dish]));
const res = sampe1map.map(x =&gt; lut.get(x));

console.log(res);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 19:15:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定