0

Hi i have an collection like this, i am trying to filter the collection,can anybdy help me on this

[{
        "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"
    }]

I need an filtered collection where cityname should present and permission:"yes", the output i need is.

Output:

[

 {
        "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"
    }]
0

2 Answers 2

3

You could use Array#filter and check the wanted constraints.

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

lodash with _.chain and _.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 = _
        .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>

Sign up to request clarification or add additional context in comments.

Comments

1

This is the code you are looking for, using filter:

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)

Filter takes a function and applies it to each element in the array, if the function returns true the item is kept, otherwise it is discarded.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.