简体   繁体   中英

How to filter array of object based on another array values using javascript?

I have a simple array of numbers (which for my app represents user Id's). For example...

[1, 2, 3]

I also have an array of objects such as

[
    {Id: 1, FirstName: "Bob", LastName: "Jones", EmailAddress: "bob@bob.com"},
    {Id: 2, FirstName: "Tessa", LastName: "Wong", EmailAddress: "tes@wong.com"},
    {Id: 3, FirstName: "Craig", LastName: "Murray", EmailAddress: "Craig@muz.com"},
    {Id: 4, FirstName: "Bryce", LastName: "Willamson", EmailAddress: "email@me.com"},
    {Id: 5, FirstName: "Tony", LastName: "Ocka", EmailAddress: "toni@oz.com"}
]

What I need to do is match any values that appear in the initial array with they key Id in the second array of objects (and return a new full array of objects containing only matches). So for example in this case, the result I'm looking for is:

[
    {Id: 1, FirstName: "Bob", LastName: "Jones", EmailAddress: "bob@bob.com"},
    {Id: 2, FirstName: "Tessa", LastName: "Wong", EmailAddress: "tes@wong.com"},
    {Id: 3, FirstName: "Craig", LastName: "Murray", EmailAddress: "Craig@muz.com"},
]

Any help would be really appreciated.

It's always good to show your attempt in OP to help fellow users to understand where you are stuck.

You can use "Array.filter" and "includes" for this.

 let arr1 = [1,2,3] let arr2 = [ {Id: 1, FirstName: "Bob", LastName: "Jones", EmailAddress: "bob@bob.com"}, {Id: 2, FirstName: "Tessa", LastName: "Wong", EmailAddress: "tes@wong.com"}, {Id: 3, FirstName: "Craig", LastName: "Murray", EmailAddress: "Craig@muz.com"}, {Id: 4, FirstName: "Bryce", LastName: "Willamson", EmailAddress: "email@me.com"}, {Id: 5, FirstName: "Tony", LastName: "Ocka", EmailAddress: "toni@oz.com"} ] let result = arr2.filter(d => arr1.includes(d.Id)) console.log(result) 

Also you can do this by iterating through the second array

 let arr1 = [1,2,3]; let arr2 = [ {Id: 1, FirstName: "Bob", LastName: "Jones", EmailAddress: "bob@bob.com"}, {Id: 2, FirstName: "Tessa", LastName: "Wong", EmailAddress: "tes@wong.com"}, {Id: 3, FirstName: "Craig", LastName: "Murray", EmailAddress: "Craig@muz.com"}, {Id: 4, FirstName: "Bryce", LastName: "Willamson", EmailAddress: "email@me.com"}, {Id: 5, FirstName: "Tony", LastName: "Ocka", EmailAddress: "toni@oz.com"} ] let result = []; for(let i in arr2) { if(arr1.indexOf(arr2[i].Id)!==-1) result.push(arr2[i]); } console.log(result); 

You can use a Set to optimize your performance. The lookup operation of an array takes O(n) which increases the overall time complexity of your function to O(n^2) whereas lookup operation in Set takes O(1) so overall time complexity is O(n).

 let arr1 = [1,2,3] let set = new Set(arr1); let arr2 = [ {Id: 1, FirstName: "Bob", LastName: "Jones", EmailAddress: "bob@bob.com"}, {Id: 2, FirstName: "Tessa", LastName: "Wong", EmailAddress: "tes@wong.com"}, {Id: 3, FirstName: "Craig", LastName: "Murray", EmailAddress: "Craig@muz.com"}, {Id: 4, FirstName: "Bryce", LastName: "Willamson", EmailAddress: "email@me.com"}, {Id: 5, FirstName: "Tony", LastName: "Ocka", EmailAddress: "toni@oz.com"}]; let result = arr2.filter(({Id}) => set.has(Id)) console.log(result) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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