简体   繁体   English

如何使用 javascript 按属性过滤另一个对象数组的对象数组

[英]How to filter array of objects by another array of objects by property using javascript

I have two nested array of objects, how to compare two array of objects by我有两个嵌套的对象数组,如何比较两个对象数组

id from arrobj1 and assignId from arrobj2 using javascript来自 arrobj1 的id和来自assignId的 assignId 使用 javascript

So, I would to know how to compare array of objects by id and assignId and return array of objects using javascript所以,我想知道如何通过 id 和 assignId 比较对象数组并使用 javascript 返回对象数组


Tried

const result =  arrobj1.filter(arr1 => {
       arrobj2.find(arr2 => arr2.assignId === arr1.id)
    });

var arrobj1 =[
 {id: 1, name: 'xxx', value:100},
 {id: 2, name: 'yyy', value:200},
 {id: 3, name: 'zzz', value:400}
]

var arrobj2 =[
 {country: 'IN', name: 'lina', assignId:2},
 {country: 'MY', name: 'john', assignId:3},
 {country: 'SG', name: 'peter', assignId:6}
]

Expected Code:

[
 {id: 2, name: 'yyy', value:200},
 {id: 3, name: 'zzz', value:400}
]

You have it almost correct, but you need to return in your filter, either by explicitly adding the return keyword or by removing the braces to use the arrow function's implicit return:您几乎是正确的,但是您需要通过显式添加return关键字或删除大括号以使用箭头函数的隐式返回来在过滤器中返回:

const result = arrobj1.filter(arr1 =>
  arrobj2.find(arr2 => arr2.assignId === arr1.id)
)
// or
const result = arrobj1.filter(arr1 => {
  return arrobj2.find(arr2 => arr2.assignId === arr1.id)
})

We can combine Array.filter() and Array.some() to make it more simple我们可以结合Array.filter()Array.some()来使它更简单

let result = arrobj1.filter(a1 => arrobj2.some(a2 => a2.assignId === a1.id) )
console.log(result)

For your code,the reason is that you have missing return when invoke find对于您的代码,原因是调用find时缺少return

 var arrobj1 =[ {id: 1, name: 'xxx', value:100}, {id: 2, name: 'yyy', value:200}, {id: 3, name: 'zzz', value:400} ] var arrobj2 =[ {country: 'IN', name: 'lina', assignId:2}, {country: 'MY', name: 'john', assignId:3}, {country: 'SG', name: 'peter', assignId:6} ] let result = arrobj1.filter(a1 => arrobj2.some(a2 => a2.assignId === a1.id) ) console.log(result)

You can generally go with the filter and some combination as @flyingfox mentioned in the answer, But if you'd have thousands of records then your time complexity would increase which you can solve by removing the nested some loop.您通常可以 go 使用filter和答案中提到的@flyingfox 的some组合,但是如果您有数千条记录,那么您的时间复杂度会增加,您可以通过删除嵌套的some循环来解决。

So more performant code would look like the following for a bigger data set.因此,对于更大的数据集,更高性能的代码看起来像下面这样。

And yes, Either use return with braces or simply remove the braces for one-liner returns!是的,要么使用带大括号的返回,要么简单地删除大括号以进行单行返回!

 var arrobj1 = [ { id: 1, name: 'xxx', value: 100 }, { id: 2, name: 'yyy', value: 200 }, { id: 3, name: 'zzz', value: 400 }, ] var arrobj2 = [ { country: 'IN', name: 'lina', assignId: 2 }, { country: 'MY', name: 'john', assignId: 3 }, { country: 'SG', name: 'peter', assignId: 6 }, ] var obj = {} for (const elem of arrobj2) { obj[elem.assignId] = true } let result = arrobj1.filter((a1) => obj[a1.id]) console.log(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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