如何按数组元素过滤集合

时间:2017-10-26 10:10:31

标签: javascript lodash

我有以下格式的数组

"categories": [
  {
     "type": "A",               
     "subtype": [
        "X",
        "Y",
        "Z",
        "D",
         "E"           
     ],

  },
  {
     "type": "B",
     "Subtypes": [
        "0",
        "1",
        "2",
        "3",
        "4",
        "5"
     ],        
  },
  {
     "type": "C",         
     "includeConnectionTypes": [
        "@",
        "#",
        "$"
     ],
}]

我有第二个数组B

B = ["C","A"]

现在如何根据数组B中的元素过滤类别数组中的元素

4 个答案:

答案 0 :(得分:1)

<强> ES5

var result = categories.filter(function (item) {
    return B.indexOf(item.type) > -1;    
});

<强> ES6

var result = categories.filter(item => B.indexOf(item.type) > -1);

此声明将检查&#34;类别&#34;的每个元素。数组,如果它的类型是数组B的元素,它将被推送到&#34;结果&#34;阵列。

&#34;的indexOf&#34; method返回数组中元素的索引,如果该数组不包含该元素,则此方法将返回-1。

参考:Array.prototype.filter()

答案 1 :(得分:1)

var categories=[
  {
     "type": "A",               
     "subtype": [
        "X",
        "Y",
        "Z",
        "D",
         "E"           
     ],

  },
  {
     "type": "B",
     "Subtypes": [
        "0",
        "1",
        "2",
        "3",
        "4",
        "5"
     ],        
  },
  {
     "type": "C",         
     "includeConnectionTypes": [
        "@",
        "#",
        "$"
     ],
}];

这是一个数组,现在B也是数组

var B = ["C","A"]

 var result=categories.filter(function(d){
    return B.indexOf(d.type)!=-1;
 });

&#34;导致&#34;包含您的预期结果。

答案 2 :(得分:0)

我认为这就是您所需要的:_.filter

_.filter(categories, (item => B.indexOf(item.type)>= 0));

答案 3 :(得分:0)

Array.prototype.filterArray.prototype.some的绑定用法的组合应该是一种易读的方法,通过像doesCategoryTypeMatchAnyBoundType这样的唯一特定函数也具有代码重用的优势,不像结合filterindexOf的其他方法/解决方案,不需要知道&#34;过滤器类型列表的引用(来自给定示例的B)。

&#13;
&#13;
// Q: I am having array in following format ...

var categories = [{
  "type": "A",
  "subtype": ["X", "Y", "Z", "D", "E"]
}, {
  "type": "B",
  "Subtypes": ["0", "1", "2", "3", "4", "5"]
}, {
  "type": "C",
  "includeConnectionTypes": ["@", "#", "$"]
}];

// ... I am having 2nd array array ...

var typeList = ["C","A"];

// ... now how to filter elements in category array based on
// elements in ... `typeList` ... array ...


// A: a combination of `Array.prototype.filter` and
// the bound usage of `Array.prototype.some` should make
// a well readable approach ...

function doesCategoryTypeMatchAnyBoundType(categoryItem) {
  return this.some(function (type) { return (categoryItem.type === type); });
}

var filteredCategoryList = categories.filter(doesCategoryTypeMatchAnyBoundType, typeList);

console.log("filteredCategoryList : ", filteredCategoryList);
&#13;
.as-console-wrapper { max-height: 100%!important; top: 0; }
&#13;
&#13;
&#13;

  

是否有任何lodash功能可以做同样的事情?

尽可能尝试坚持使用语言核心。但是,如果您被迫使用 lodash ,那么刚提供的方法将改为......

&#13;
&#13;
var categories = [{
  "type": "A",
  "subtype": ["X", "Y", "Z", "D", "E"]
}, {
  "type": "B",
  "Subtypes": ["0", "1", "2", "3", "4", "5"]
}, {
  "type": "C",
  "includeConnectionTypes": ["@", "#", "$"]
}];

var typeList = ["C","A"];


function doesCategoryTypeMatchAnyBoundType(categoryItem) {
  return _.some(this, function (type) { return (categoryItem.type === type); });
}

var filteredCategoryList = _.filter(categories, doesCategoryTypeMatchAnyBoundType.bind(typeList));

console.log("filteredCategoryList : ", filteredCategoryList);
&#13;
.as-console-wrapper { max-height: 100%!important; top: 0; }
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
&#13;
&#13;
&#13;

相关问题