过滤 JSON 对象并得到第二个数组作为结果
filter JSON object and get a second array as result
有一个JSON
对象,它的结构如图所示。我如何通过名为 "State"
的键过滤此对象并得到第二个数组 ??
为了清楚起见,我需要一个与此 MySQL 代码相同的翻译代码:SELECT * FROM data.sepah WHERE state = 'sth'
谁能帮我解决这个问题?
我的完整 JSON 文件是这样的:
{
"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"
}
]
}
你可以简单地做 map
:
const newArray = theArray.map(item => item.state);
或者,在 Safari 和所有尚不支持 ES6 的浏览器中,
var newArray = theArray.map(function (item) {
return item.state;
});
这将 return 一个只包含字符串的数组,每个字符串取自 state
属性 输入数组的每一项。
你可以使用 Array#filter
。
result = data.sepah.filter(function (a) {
return a.state === 'somevalue';
});
工作示例:
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>');
有独立功能:
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>');
有多个值:
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>');
如果您希望过滤以仅匹配某些特定的 "state" 值,您应该这样做:
var newArr = obj.sepah.filter(function (i) {
return i.state === 'sth';
});
有一个JSON
对象,它的结构如图所示。我如何通过名为 "State"
的键过滤此对象并得到第二个数组 ??
为了清楚起见,我需要一个与此 MySQL 代码相同的翻译代码:SELECT * FROM data.sepah WHERE state = 'sth'
谁能帮我解决这个问题?
我的完整 JSON 文件是这样的:
{
"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"
}
]
}
你可以简单地做 map
:
const newArray = theArray.map(item => item.state);
或者,在 Safari 和所有尚不支持 ES6 的浏览器中,
var newArray = theArray.map(function (item) {
return item.state;
});
这将 return 一个只包含字符串的数组,每个字符串取自 state
属性 输入数组的每一项。
你可以使用 Array#filter
。
result = data.sepah.filter(function (a) {
return a.state === 'somevalue';
});
工作示例:
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>');
有独立功能:
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>');
有多个值:
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>');
如果您希望过滤以仅匹配某些特定的 "state" 值,您应该这样做:
var newArr = obj.sepah.filter(function (i) {
return i.state === 'sth';
});