【问题标题】:javascript filter object & arrayjavascript过滤器对象和数组
【发布时间】:2018-04-27 03:44:01
【问题描述】:

你能帮我过滤这些数据吗?

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

我怎样才能得到过滤后的结果? 谢谢

【问题讨论】:

  • 到目前为止你有没有尝试过?如果您需要调试帮助,请发布它。
  • 什么“过滤结果”?如何过滤?
  • 旁注:变量名arr 令人困惑……它是一个对象,而不是数组。
  • @MattBrowne 也许是盗版变量?
  • @Phil Lol... arrrr

标签: javascript arrays filter


【解决方案1】:

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

返回函数的函数使用称为柯里化/闭包的东西并且可以部分应用(如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]))
)

【讨论】:

  • 不错!谢谢你:)
猜你喜欢
  • 1970-01-01
  • 2012-11-15
  • 2019-10-15
  • 2022-07-13
  • 1970-01-01
  • 2018-07-17
  • 1970-01-01
  • 2017-10-19
相关资源
最近更新 更多