如何使用lodash或JavaScript提取过滤的对象

时间:2017-02-01 20:02:04

标签: javascript arrays underscore.js lodash

嗨我有这样的收藏品,我正在尝试过滤收藏品,anybdy可以帮我解决这个问题

[{
        "parent_id": "DISTRIBUTOR1",
        "id": "DISTRIBUTOR3",
        "permission": "Yes",
        "country_name": "India"
    },
    {
        "parent_id": "DISTRIBUTOR2",
        "id": "DISTRIBUTOR3",
        "permission": "NO",
        "country_name": "India"
    },
    {
        "parent_id": "DISTRIBUTOR2",
        "id": "DISTRIBUTOR3",
        "permission": "YES",
        "city_name": "Hubli",
        "state_name": "Karnataka",
        "country_name": "India"
    },
      {
        "parent_id": "DISTRIBUTOR2",
        "id": "DISTRIBUTOR3",
        "permission": "YES",
        "city_name": "Bangalore",
        "state_name": "Karnataka",
        "country_name": "India"
    }]

我需要一个过滤的集合,其中 cityname 应该出现,权限:“是”,我需要的输出。

输出:

[

 {
        "parent_id": "DISTRIBUTOR2",
        "id": "DISTRIBUTOR3",
        "permission": "YES",
        "city_name": "Hubli",
        "state_name": "Karnataka",
        "country_name": "India"
    },
      {
        "parent_id": "DISTRIBUTOR2",
        "id": "DISTRIBUTOR3",
        "permission": "YES",
        "city_name": "Bangalore",
        "state_name": "Karnataka",
        "country_name": "India"
    }]

2 个答案:

答案 0 :(得分:3)

您可以使用Array#filter并检查所需的约束。



var array = [{ parent_id: "DISTRIBUTOR1", id: "DISTRIBUTOR3", permission: "Yes", country_name: "India" }, { parent_id: "DISTRIBUTOR2", id: "DISTRIBUTOR3", permission: "NO", country_name: "India" }, { parent_id: "DISTRIBUTOR2", id: "DISTRIBUTOR3", permission: "YES", city_name: "Hubli", state_name: "Karnataka", country_name: "India" }, { parent_id: "DISTRIBUTOR2", id: "DISTRIBUTOR3", permission: "YES", city_name: "Bangalore", state_name: "Karnataka", country_name: "India" }],
    result = array.filter(function (a) {
        return a.city_name && a.permission === 'YES';
    });

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }




使用_.chain_.filter

进行lodash



var array = [{ parent_id: "DISTRIBUTOR1", id: "DISTRIBUTOR3", permission: "Yes", country_name: "India" }, { parent_id: "DISTRIBUTOR2", id: "DISTRIBUTOR3", permission: "NO", country_name: "India" }, { parent_id: "DISTRIBUTOR2", id: "DISTRIBUTOR3", permission: "YES", city_name: "Hubli", state_name: "Karnataka", country_name: "India" }, { parent_id: "DISTRIBUTOR2", id: "DISTRIBUTOR3", permission: "YES", city_name: "Bangalore", state_name: "Karnataka", country_name: "India" }],
    result = _
        .chain(array)
        .filter('city_name')
        .filter({ permission: 'YES' });

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这是您要查找的代码,使用filter

&#13;
&#13;
var arr = [{
            "parent_id": "DISTRIBUTOR1",
            "id": "DISTRIBUTOR3",
            "permission": "Yes",
            "country_name": "India"
        },
        {
            "parent_id": "DISTRIBUTOR2",
            "id": "DISTRIBUTOR3",
            "permission": "NO",
            "country_name": "India"
        },
        {
            "parent_id": "DISTRIBUTOR2",
            "id": "DISTRIBUTOR3",
            "permission": "YES",
            "city_name": "Hubli",
            "state_name": "Karnataka",
            "country_name": "India"
        },
          {
            "parent_id": "DISTRIBUTOR2",
            "id": "DISTRIBUTOR3",
            "permission": "YES",
            "city_name": "Bangalore",
            "state_name": "Karnataka",
            "country_name": "India"
        }]
    
    var filtered = arr.filter(function(item) {
      return item.city_name && item.permission === "YES"
    })
    
    console.log(filtered)
&#13;
&#13;
&#13;

Filter接受一个函数并将其应用于数组中的每个元素,如果函数返回true,则保留该项,否则将被丢弃。