-1

There is a JSON object and it's structure has shown in the picture. how could i filter this object by a Key named "State" and as result get a second array ??

to make it clear, i need a translated code as equal as this MySQL code : SELECT * FROM data.sepah WHERE state = 'sth'

could anyone help me out solve this problem ?

enter image description here

And My full JSON file is like this :

{

"sepah": [

    {  
        "row"   : "1",     
        "city"    : "newYork",
        "yegan" : "california",
        "raste" : "tese",
        "clinic" : "+",
        "degree" :  "1",
        "notation" : "empty",
        "kind"  :   "airforce",
        "state" :   "azerbaijan-east"


    },
    {  
        "row"   : "1",     
        "city"    : "اصفهان",
        "yegan" : "۸۲ امام حسین",
        "raste" : "پدافندی",
        "clinic" : "دارد",
        "degree" :  "۱",
        "notation" : "دو مرکز درمانی",
        "kind"  :   "نیروی هوایی",
        "state" :   "azerbaijan-east"


    },
    {  
        "row"   : "1",     
        "city"    : "اصفهان",
        "yegan" : "۸۲ امام حسین",
        "raste" : "پدافندی",
        "clinic" : "دارد",
        "degree" :  "۱",
        "notation" : "دو مرکز درمانی",
        "kind"  :   "نیروی هوایی",
        "state" :   "azerbaijan-east"


    },
    {  
        "row"   : "1",     
        "city"    : "اصفهان",
        "yegan" : "۸۲ امام حسین",
        "raste" : "پدافندی",
        "clinic" : "دارد",
        "degree" :  "۱",
        "notation" : "دو مرکز درمانی",
        "kind"  :   "نیروی هوایی",
        "state" :   "azerbaijan-east"


    }

],

"naja": [

    {  
        "id"   : "1",     
        "city"    : "ارتش هرمزگان",
        "far_entz_ostan"    : "۸۲ امام حسین",
        "far_entz_shahr"    : "پدافندی",
        "far_marzbani" : "دارد",
        "hang_marzi" :  "۱",
        "yegan_vije" : "دو مرکز درمانی",
        "clinic"    : "ارتش",
        "hospital"  :   "نیروی هوایی",
        "markaz_amoozeshi"  :   "خراسان رضوی",
        "kind"  :   "نیروی هوایی",          
        "state" :   "azerbaijan-east"


    },
    {  
        "id"   : "1",     
        "city"    : "ارتش هرمزگان",
        "far_entz_ostan"    : "۸۲ امام حسین",
        "far_entz_shahr"    : "پدافندی",
        "far_marzbani" : "دارد",
        "hang_marzi" :  "۱",
        "yegan_vije" : "دو مرکز درمانی",
        "clinic"    : "ارتش",
        "hospital"  :   "نیروی هوایی",
        "markaz_amoozeshi"  :   "خراسان رضوی",
        "state" :   "azerbaijan-east"

    },
    {  
        "id"   : "1",     
        "city"    : "ارتش هرمزگان",
        "far_entz_ostan"    : "۸۲ امام حسین",
        "far_entz_shahr"    : "پدافندی",
        "far_marzbani" : "دارد",
        "hang_marzi" :  "۱",
        "yegan_vije" : "دو مرکز درمانی",
        "clinic"    : "ارتش",
        "hospital"  :   "نیروی هوایی",
        "markaz_amoozeshi"  :   "خراسان رضوی",
        "kind"  :   "نیروی هوایی",          
        "state" :   "azerbaijan-east"

    },
    {  
        "id"   : "1",     
        "city"    : "ارتش هرمزگان",
        "far_entz_ostan"    : "۸۲ امام حسین",
        "far_entz_shahr"    : "پدافندی",
        "far_marzbani" : "دارد",
        "hang_marzi" :  "۱",
        "yegan_vije" : "دو مرکز درمانی",
        "clinic"    : "ارتش",
        "hospital"  :   "نیروی هوایی",
        "markaz_amoozeshi"  :   "خراسان رضوی",
        "kind"  :   "نیروی هوایی",          
        "state" :   "azerbaijan-east"


    }

],

"aja": [

    {  
        "id"   : "1",     
        "city"    : "اصفهان",
        "yegan" : "۸۲ امام حسین",
        "raste" : "پدافندی",
        "clinic" : "دارد",
        "degree" :  "۱",
        "notation" : "دو مرکز درمانی",
        "kind"  :   "نیروی هوایی",
        "state" :   "azerbaijan-east"


    },
    {  
        "id"   : "1",     
        "city"    : "اصفهان",
        "yegan" : "۸۲ امام حسین",
        "raste" : "پدافندی",
        "clinic" : "دارد",
        "degree" :  "۱",
        "notation" : "دو مرکز درمانی",
        "kind"  :   "نیروی هوایی",
        "state" :   "azerbaijan-east"


    },
    {  
        "id"   : "1",     
        "city"    : "اصفهان",
        "yegan" : "۸۲ امام حسین",
        "raste" : "پدافندی",
        "clinic" : "دارد",
        "degree" :  "۱",
        "notation" : "دو مرکز درمانی",
        "kind"  :   "نیروی هوایی",
        "state" :   "azerbaijan-east"


    },
    {  
        "id"   : "1",     
        "city"    : "اصفهان",
        "yegan" : "۸۲ امام حسین",
        "raste" : "پدافندی",
        "clinic" : "دارد",
        "degree" :  "۱",
        "notation" : "دو مرکز درمانی",
        "kind"  :   "نیروی هوایی",
        "state" :   "azerbaijan-east"


    }

],

"setad_kol_sepah": [

    {  
        "id"   : "1",     
        "city"    : "اصفهان",
        "sepah_ostani"  : "۸۲ امام حسین",
        "nahiye_moghavemat" : "پدافندی",
        "tip_mardom_paye" : "دارد",
        "darmangah" :   "۱",
        "daraje" : "دو مرکز درمانی",
        "markaz_amoozeshi"  : "ارتش",
        "state" :   "azerbaijan-east"


    },
    {  
        "id"   : "1",     
        "city"    : "اصفهان",
        "sepah_ostani"  : "۸۲ امام حسین",
        "nahiye_moghavemat" : "پدافندی",
        "tip_mardom_paye" : "دارد",
        "darmangah" :   "۱",
        "daraje" : "دو مرکز درمانی",
        "markaz_amoozeshi"  : "ارتش",
        "state" :   "azerbaijan-east"

    },
    {  
        "id"   : "1",     
        "city"    : "اصفهان",
        "sepah_ostani"  : "۸۲ امام حسین",
        "nahiye_moghavemat" : "پدافندی",
        "tip_mardom_paye" : "دارد",
        "darmangah" :   "۱",
        "daraje" : "دو مرکز درمانی",
        "markaz_amoozeshi"  : "ارتش",
        "state" :   "azerbaijan-east"


    },
    {  
        "id"   : "1",     
        "city"    : "اصفهان",
        "sepah_ostani"  : "۸۲ امام حسین",
        "nahiye_moghavemat" : "پدافندی",
        "tip_mardom_paye" : "دارد",
        "darmangah" :   "۱",
        "daraje" : "دو مرکز درمانی",
        "markaz_amoozeshi"  : "ارتش",
        "state" :   "azerbaijan-east"


    }

]    

}

Sami
  • 129
  • 2
  • 16
  • do you want to get an array of all nested properties that contain the text "state"? – Calvin Belden Mar 14 '16 at 21:48
  • Are there any "state" properties only in sepah, or also in naja and aja, etc? What should be the expected result? I'm pretty sure that, if you take the time to write down the result, you can get away with a solution just using a simple for and Array.push. – Alberto Chiesa Mar 14 '16 at 21:49
  • Duplicate question: [How to filter object array based on attributes?](http://stackoverflow.com/questions/2722159/javascript-how-to-filter-object-array-based-on-attributes) – Yogi Mar 14 '16 at 21:53
  • consider if i want to get all 'sepah' result , it could be accessed by a var named data.sepah in javascript ,,,, but i only want ones which their state is equal to X value – Sami Mar 14 '16 at 21:54
  • to make it clear, i need a translated code as equal as this MySQL code : `SELECT * FROM data.sepah WHERE state = 'sth' ` – Sami Mar 14 '16 at 21:57

3 Answers3

1

If you wish to filter to match only some specific "state" value, you should do:

var newArr = obj.sepah.filter(function (i) {
  return i.state === 'sth';
});
Hugo W.
  • 84
  • 4
0

You can simply do map:

const newArray = theArray.map(item => item.state);

or, in Safari and all those browsers that don't yet support ES6,

var newArray = theArray.map(function (item) {
  return item.state;
});

This will return an array that only consists of strings, each taken from state property of every item of the input array.

rishat
  • 8,206
  • 4
  • 44
  • 69
  • Besides, I'd strongly recommend using [lodash](https://lodash.com). Lodash provides type-safe `map` and `get` functions, first to map through an array (or array-like), second safely retrieves an object property by path to this property. – rishat Mar 14 '16 at 21:52
0

You can use Array#filter for that.

result = data.sepah.filter(function (a) {
    return a.state === 'somevalue';
});

Working example:

var data = { "sepah": [{ "row": "1", "city": "newYork", "yegan": "california", "raste": "tese", "clinic": "+", "degree": "1", "notation": "empty", "kind": "airforce", "state": "azerbaijan-east" }, { "row": "2", "city": "??????", "yegan": "?? ???? ????", "raste": "???????", "clinic": "????", "degree": "?", "notation": "?? ???? ??????", "kind": "????? ?????", "state": "azerbaijan-west" }, ], "naja": [], "setad_kol_sepah": [] },
    result = data.sepah.filter(function (a) {
        return a.state === 'azerbaijan-east';
    });

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

With separate function:

function filter(array, key, search) {
    return array.filter(function (a) {
        return a[key] === search;
    });
}

var data = { "sepah": [{ "row": "1", "city": "newYork", "yegan": "california", "raste": "tese", "clinic": "+", "degree": "1", "notation": "empty", "kind": "airforce", "state": "azerbaijan-east" }, { "row": "2", "city": "??????", "yegan": "?? ???? ????", "raste": "???????", "clinic": "????", "degree": "?", "notation": "?? ???? ??????", "kind": "????? ?????", "state": "azerbaijan-west" }, ], "naja": [], "setad_kol_sepah": [] },
    result = filter(data.sepah, 'state', 'azerbaijan-east');

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

With more than one value:

function filter(array, key, searchArray) {
    return array.filter(function (a) {
        return ~searchArray.indexOf(a[key]);
    });
}

var data = { "sepah": [{ "row": "1", "city": "newYork", "yegan": "california", "raste": "tese", "clinic": "+", "degree": "1", "notation": "empty", "kind": "airforce", "state": "azerbaijan-east" }, { "row": "2", "city": "??????", "yegan": "?? ???? ????", "raste": "???????", "clinic": "????", "degree": "?", "notation": "?? ???? ??????", "kind": "????? ?????", "state": "azerbaijan-west" }, ], "naja": [], "setad_kol_sepah": [] },
    result = filter(data.sepah, 'state', ['azerbaijan-east', 'azerbaijan-west']);

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Worked perfectly :) thnx... if i want to put a variable instead of 'azerbaijan-east' , how should it be passed to function ? – Sami Mar 14 '16 at 22:09
  • very well, and as my last question what should i do for 2 search values ? – Sami Mar 14 '16 at 22:18