从JSONArray中select所有包含的JSONObjects

我有一个JSONArray包含JSONObjects像这样{firstname:'John', lastname:'Doe'}

有没有办法直接select所有具有firstname == 'John' JSONObjects ,或者唯一的方法是循环数组,testing字段firstname并将所有匹配对象保存在另一个数组中?

谢谢

过滤方法应该做的伎俩。

 var jsonArrayString = '[{"firstname": "John","otherProp": "otherValue"},{"firstname": "other Name", "otherProp": "otherValue"}]'; var jsonArray = JSON.parse(jsonArrayString); var filteredArray = jsonArray.filter(function(element) { return element.firstname === "John"; }); console.log(filteredArray); 

你可以过滤掉它们:

 var people = JSONArray.filter(function(obj) { return obj.firstname == "John"; }); 

仅供参考:您有一个包含对象数组