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
Post a Comment