如何找到特定的对象,并将它们放入JavaScript中的数组中 [英] how to find specific objects and put them in an array in javascript
本文介绍了如何找到特定的对象,并将它们放入JavaScript中的数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的脚本中有这个结果
'[{region:NCA,depprt:古巴哈瓦那,{区域:NCA,depprt:古巴哈瓦那},{区域:NCA,depprt:牙买加蒙特哥湾}, :NCA,depprt:牙买加蒙特哥贝}]'
$ b
var jsonList ='@ Html.Raw(Json.Encode(ViewBag.chk))'
var jsList = JSON.stringify(jsonList);
代表 jsList
我想获得所有 depprt
,其中 region
等于 NCA
.how能否做到这一点。
var ncaList = jsonList.filter(function(obj){return obj.region ==NCA;});
I have this result in my script
'[{"region":"NCA","depprt":"Havana, Cuba"},{"region":"NCA","depprt":"Havana, Cuba"},{"region":"NCA","depprt":"Montego Bay, Jamaica"},{"region":"NCA","depprt":"Montego Bay, Jamaica"}]'
this is the code to get it.
var jsonList = '@Html.Raw(Json.Encode(ViewBag.chk))'
var jsList = JSON.stringify(jsonList);
for jsList
I got above result.now I want to get all depprt
where region
is equal to NCA
.how can I do that.
解决方案
You can use the .filter() method for this.
var ncaList = jsonList.filter(function(obj){ return obj.region == "NCA"; });
这篇关于如何找到特定的对象,并将它们放入JavaScript中的数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文