Как отфильтровать коллекцию по элементам массива

У меня есть массив в следующем формате

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

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

У меня 2-й массив массив B

B = ["C","A"]

Теперь, как фильтровать элементы в массиве категорий на основе элементов в массиве B

4 ответа

Решение

Я думаю, что это то, что вам нужно: _.filter,

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

ES5

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

ES6

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

Этот оператор проверит каждый элемент массива "category", если его тип является элементом массива B, он будет помещен в массив "result".

Метод indexOf возвращает индекс элемента в массиве, и если этот массив не содержит этот элемент, этот метод вернет -1.

Ссылка: Array.prototype.filter ()

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;
 });

"результат" содержит ожидаемый результат.

Сочетание Array.prototype.filter и связанное использование Array.prototype.some должен делать хорошо читаемый подход, который также имеет преимущество повторного использования кода через единственную специфическую функцию, такую ​​как doesCategoryTypeMatchAnyBoundType что, в отличие от других подходов / решений, которые объединяют filter а также indexOf, не нужно "знать" ссылку на список типов фильтров (там B из приведенного примера).

// 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);
.as-console-wrapper { max-height: 100%!important; top: 0; }

есть ли функция lodash, чтобы сделать то же самое?

Попробуйте придерживаться языкового ядра как можно дольше. Но если вы вынуждены использовать lodash, только что предоставленный подход изменится на...

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);
.as-console-wrapper { max-height: 100%!important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Другие вопросы по тегам