Pex
Pex

Reputation: 519

UnderscoreJS: Comparing two arrays

I currently have two arrays which I would like to compare with one and other:

Array one:

var allCategories =
[
 { utilization:"license", id:"450", type: "MainCategory" },
 { utilization:"rating", id:"451", type: "SubCategory" },
 { utilization:"medical", id:"452", type: "MainCategory" },
 { utilization:"other", id:"453", type: "MainCategory" }
];

Array Two:

var allKinds = 
[
 { name:"name1", id:"450", type: "FAA" },
 { name:"name2", id:"451", type: "FAA" },
 { name:"name3", id:"451", type: "FAA" },
 { name:"name4", id:"451", type: "FAA" },
 { name:"name5", id:"451", type: "SPA" },
 { name:"name6", id:"451", type: "SPA" },
 { name:"name7", id:"452", type: "FBC" },
 { name:"name8", id:"453", type: "SPA" }
];

What I am trying to do, is compare retrieve the items from "Array Two" with the utilizations: "license", "medical" & "other".

Currently I have already extracted the IDs from "Array One" as such:

var categories = _.filter(allCategories, function(obj) {
  return obj.utilization === 'license' || obj.utilization === 'medical' || obj.utilization === 'other';
});
var category_ids = _.pluck(categories, 'id');

The second part, I can't seem to figure out: How do I compare the result of "Array one" [450, 452, 453] with the items in "Array Two" to retrieve the items with the id's.

Here is a jsfiddle: https://jsfiddle.net/7n9pjrc9/

Upvotes: 1

Views: 292

Answers (1)

nikoshr
nikoshr

Reputation: 33364

A simple solution would be to filter your list by checking if category_ids contains the object id :

var r1 = _.filter(allKinds, function(o) {
    return _.contains(category_ids, o.id);
});
console.log(r1);

Or if you prefer, filter against a hash of your selected ids

var hashed = {};
_.each(categories, function(o) {
    hashed[o.id] = true;
});
var r2 = _.filter(allKinds, function(o) {
    return hashed[o.id];
});
console.log(r2);

And a demo

var allCategories =
[
 { utilization:"license", id:"450", type: "MainCategory" },
 { utilization:"rating", id:"451", type: "SubCategory" },
 { utilization:"medical", id:"452", type: "MainCategory" },
 { utilization:"other", id:"453", type: "MainCategory" }
];

var allKinds = 
[
 { name:"name1", id:"450", type: "FAA" },
 { name:"name2", id:"451", type: "FAA" },
 { name:"name3", id:"451", type: "FAA" },
 { name:"name4", id:"451", type: "FAA" },
 { name:"name5", id:"451", type: "SPA" },
 { name:"name6", id:"451", type: "SPA" },
 { name:"name7", id:"452", type: "FBC" },
 { name:"name8", id:"453", type: "SPA" }
];


var categories = _.filter(allCategories, function(obj) {
  return obj.utilization === 'license' || obj.utilization === 'medical' || obj.utilization === 'other';
});
var category_ids = _.pluck(categories, 'id');

var r1 = _.filter(allKinds, function(o) {
	return _.contains(category_ids, o.id);
});
console.log(r1);

var hashed = {};
_.each(categories, function(o) {
	hashed[o.id] = true;
});
var r2 = _.filter(allKinds, function(o) {
	return hashed[o.id];
});
console.log(r2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

Upvotes: 2

Related Questions