UnderscoreJS:比较两个数组
UnderscoreJS: Comparing two arrays
我目前有两个数组,我想将它们与另一个数组进行比较:
数组一:
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" }
];
我想做的是比较从 "Array Two" 中检索的项目与利用率:"license"、"medical" 和 "other".
目前我已经从 "Array One" 中提取了 ID:
var categories = _.filter(allCategories, function(obj) {
return obj.utilization === 'license' || obj.utilization === 'medical' || obj.utilization === 'other';
});
var category_ids = _.pluck(categories, 'id');
第二部分,我好像想不通:
如何将 "Array one" [450, 452, 453]
的结果与 "Array Two" 中的项目进行比较以检索具有 id 的项目。
这是一个 jsfiddle:
https://jsfiddle.net/7n9pjrc9/
一个简单的解决方案是通过检查 category_ids
是否包含对象 id
来过滤您的列表:
var r1 = _.filter(allKinds, function(o) {
return _.contains(category_ids, o.id);
});
console.log(r1);
或者,如果您愿意,可以根据您选择的 ID 的哈希值进行过滤
var hashed = {};
_.each(categories, function(o) {
hashed[o.id] = true;
});
var r2 = _.filter(allKinds, function(o) {
return hashed[o.id];
});
console.log(r2);
还有一个演示
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>
我目前有两个数组,我想将它们与另一个数组进行比较:
数组一:
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" }
];
我想做的是比较从 "Array Two" 中检索的项目与利用率:"license"、"medical" 和 "other".
目前我已经从 "Array One" 中提取了 ID:
var categories = _.filter(allCategories, function(obj) {
return obj.utilization === 'license' || obj.utilization === 'medical' || obj.utilization === 'other';
});
var category_ids = _.pluck(categories, 'id');
第二部分,我好像想不通:
如何将 "Array one" [450, 452, 453]
的结果与 "Array Two" 中的项目进行比较以检索具有 id 的项目。
这是一个 jsfiddle: https://jsfiddle.net/7n9pjrc9/
一个简单的解决方案是通过检查 category_ids
是否包含对象 id
来过滤您的列表:
var r1 = _.filter(allKinds, function(o) {
return _.contains(category_ids, o.id);
});
console.log(r1);
或者,如果您愿意,可以根据您选择的 ID 的哈希值进行过滤
var hashed = {};
_.each(categories, function(o) {
hashed[o.id] = true;
});
var r2 = _.filter(allKinds, function(o) {
return hashed[o.id];
});
console.log(r2);
还有一个演示
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>