部分阵列过滤器

时间:2016-08-06 06:53:12

标签: javascript lodash

我得到了以下对象数组

[
        {
                "name": "Jack Daniel",
                "job": "Software Developer",
                "age" : "40M"
        },
        {
                "name": "Sofia Daniel",
                "job": "Web Developer",
                "age" : "35F"
        },
        {
                "name": "John Smith",
                "job": "Marketing",
                "age" : "32M"
        },
        {
                "name": "Clara Johnson",
                "job": "Designer",
                "age" : "35F"
        }
]

现在使用lodash我可以过滤年龄或我喜欢的任何属性,但我怎样才能实现以下目标。

  1. 搜索35并获取年龄35F
  2. 的所有对象的列表
  3. 搜索Daniel并获取名称为Daniel的所有对象?
  4. 我觉得这很简单。

1 个答案:

答案 0 :(得分:0)

您可以在纯JavaScript中使用filter() indexOf()

var data = [{"name":"Jack Daniel","job":"Software Developer","age":"40M"},{"name":"Sofia Daniel","job":"Web Developer","age":"35F"},{"name":"John Smith","job":"Marketing","age":"32M"},{"name":"Clara Johnson","job":"Designer","age":"35F"}]

var result = data.filter(function(e) {
  return e.age.indexOf(35) != -1 && e.name.toLowerCase().indexOf('daniel') != -1;
})

console.log(result);