Javascript / JQuery Array of objects intersection by property -


given 2 arrays:

var users = [{ name: 'alice', typeid: 1 }, { name: 'bob', typeid: 2 }, { name: 'carol', typeid: 3 }]; var authorized = [{ typeid: 1 }, { typeid: 2 }]; 

i know simplest way users having typeid present in authorized array.

in case result should be:

[{ name: 'alice', typeid: 1 }, { name: 'bob', typeid: 2 }] 

var result = users.filter(function(user) {   return authorized.some(function(authorizedobj) {     return authorizedobj.typeid === user.typeid;   }); }) 

should noted if authorized array contained id's , not objects (something [1, 2]), solution simpler:

var result = users.filter(function(user) {   return authorized.indexof(user.typeid) !== -1; }) 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -