-1

I have data in JSON format as

"data1": [
    {
        "id": "01",
        "loc": "India",
        "count": "2"
    },
    {
        "id": "02",
        "loc": "India",
        "count": "200"
    },
    {
        "id": "01",
        "loc": "New York",
        "count": "500"
    }
]

I want to retrieve count with the condition that

"id" = "loc" = and the count should get retrieved

For e.g. If will give the condition as "id" = "01" and "loc" = "New York" it should retrieve "count" = "500"

halfer
  • 19,824
  • 17
  • 99
  • 186
  • For this you don't need angularjs. You can just do a JSON.parse() on that data and can retrieve the object with id and loc and then you'll get the count itself. – Krishnandu Sarkar Feb 13 '17 at 09:49
  • Possible duplicate of [Get JavaScript object from array of objects by value or property](http://stackoverflow.com/questions/13964155/get-javascript-object-from-array-of-objects-by-value-or-property) – JJJ Feb 13 '17 at 09:49
  • @user7276674 You may check https://jsfiddle.net/cwq1bkc9/ – Krishnandu Sarkar Feb 13 '17 at 10:07

1 Answers1

0

You can try JavaScript array.filter() method.

Working demo :

var data1 = [
    {
        "id": "01",
        "loc": "India",
        "count": "2"
    },
    {
        "id": "02",
        "loc": "India",
        "count": "200"
    },
    {
        "id": "01",
        "loc": "New York",
        "count": "500"
    }
];

var res = data1.filter(function(item) {
  return (item.id == '01' && item.loc == 'New York') ? item.count : null;
});

console.log(res[0].count); // 500
Debug Diva
  • 26,058
  • 13
  • 70
  • 123