首页 > javascript如何获取json有相同属性的数组并显示该数组

javascript如何获取json有相同属性的数组并显示该数组

这是数据:
{

"city": "Birmingham",
"state": "AL",
"country": "US",
"lat": "33.5206608",
"lng": "-86.80249",
"status": "Delivered",
"occured_at": "2012-04-06 14:17:00 UTC"

},
{

"city": "Birmingham",
"state": "AL",
"country": "US",
"lat": "33.5206608",
"lng": "-86.80249",
"status": "Out For Delivery",
"occured_at": "2012-04-06 04:44:00 UTC"

},
{

"city":"Spartanburg",
"state":"SC",
"country":"US",
"lat":"34.9495672",
"lng":"-81.9320482",
"status":"Departure Scan",
"occured_at":"2012-04-05 08:42:00 UTC"

}

我想要获取相同city的数组,并显示出来。我该怎么写,求助!


var json = [{

"city": "Birmingham",
"state": "AL",
"country": "US",
"lat": "33.5206608",
"lng": "-86.80249",
"status": "Delivered",
"occured_at": "2012-04-06 14:17:00 UTC"
},
{

"city": "Birmingham",
"state": "AL",
"country": "US",
"lat": "33.5206608",
"lng": "-86.80249",
"status": "Out For Delivery",
"occured_at": "2012-04-06 04:44:00 UTC"
},
{

"city":"Spartanburg",
"state":"SC",
"country":"US",
"lat":"34.9495672",
"lng":"-81.9320482",
"status":"Departure Scan",
"occured_at":"2012-04-05 08:42:00 UTC"
}];
var new_arr = [];
for(var i=1,i<json.length;++i) {
    if(json[0].city === json[i].city) {
        new_arr.push(json[i]);
    }
}
if(new_arr) {
    new_arr.unshift(json[0]);
}
console.log(new_arr);
【热门文章】
【热门文章】