javascript过滤器对象&排列

时间:2018-04-27 03:44:01

标签: javascript arrays filter

你可以帮助我过滤这些数据吗?

var arr = {
    "data": [
        {
            "name": "a",
            "tags": [1,2,3,4],
        },
        {
            "name": "b",
            "tags": [5,6,7,8],
        },
    ]
};
var filter = {
    "tags" : [6,7]
};

var res = 'result';

console.log(arr.data);
console.log(filter);
console.log(res);

我如何获得过滤结果? 感谢

1 个答案:

答案 0 :(得分:1)

由于Array.filter是一个接受函数的函数,因此我举例说明了如何使用更高阶函数(带函数和/或返回函数的函数)。

返回函数的函数使用一个名为currying / closure的东西,可以部分应用(如const plus = a=>b=>a+b; const plusTen=plus(10); const eleven=plusTen(1)

以下是示例。

const arr = [
  {
    "name": "a",
    "tags": [1, 2, 3, 4],
  },
  {
    "name": "b",
    "tags": [5, 6, 7, 8],
  },
];

//function to get an item from an object (in this case tags)
//  defaults to empty array if o or o.tags doesn't exist
const getTags = o => (o&&o.tags) || []; 

//function to compare a value, in this case array of numbers (haystack)
//  contains all of the array of number (needles)
//  in this case the haystack is tags and needles are the values we are looking for
//  returns true if all the needles are in the haystack or false if one needle is not found
const compareAllPrimitive = (hayStack,needles) =>
  needles.reduce(
    (all,needle)=>
      (all)
        ? hayStack.includes(needle)
        : all
    ,true
  );
//similar to compareAllPrimitive but will return true if any of needle(s) are found in the 
//  haystack
const compareSomePrimitive = (hayStack,needles) =>
  needles.reduce(
    (all,needle)=>
      (all)
        ? hayStack.includes(needle)
        : all
    ,false
  );
//curryable function to filter, takes 2 functions:
//  getter: to get the value to compare from the object passed into it 
//    like getTags to get tags from an item
//  comparer: to compare the value 
const filterFn = getter => comparer => object =>
  //in this case the comparer will be a partially applied function that 
  //  takes needles and haystack: needles => haystack => needles in haystack
  //  the function will already contain needles when passed in so only needs
  //  haystack to return true or false
  comparer(getter(object))
//partially applied filterFn getting the tags property from an object
const filterTags = filterFn(getTags);
//curryable function to see if all tagsToFind are in allTags
const compareAllTags = tagsToFind => allTags =>
  compareAllPrimitive(allTags,tagsToFind);
//partially applied filterFn that has getter and comparer, only need to pass the object in
const hasAllTags = whatTags=>filterTags(compareAllTags(whatTags));

//use hasAllTags filter function
console.log(
  "has all [1,2]",
  //array filer takes a function as argument, details can be found here:
  //https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
  arr.filter(hasAllTags([1,2]))
);

//partially applied filterFn that has getter and comparer, only need to pass the object in
const hasSomeTags = whatTags=>
  filterTags(
    tagsToFind => allTags =>//can pass in a function without assigning it to a constant first
      compareSomePrimitive(allTags,tagsToFind)
  );
//use hasSomeTags filter function
console.log(
  "has some [1,5]",
  arr.filter(hasSomeTags([1,5]))
)