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 ?
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"
}
]
}
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>');