本文介绍了JSON元素选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的JSON对象如下:
my JSON objects look like this:
[{"aid":"1","atitle":"Ameya R. Kadam"},{"aid":"2","atitle":"Amritpal Singh"},{"aid":"3","atitle":"Anwar Syed"},{"aid":"4","atitle":"Aratrika"},{"aid":"5","atitle":"Bharti Nagpal"}]
如您所见,名称通过其相关的辅助名称来区分.现在假设我要显示堆叠在一起的名称:4.我应该为此写什么js?
As you can see the names are differentiated through their associated aid's. Now suppose I want to display the name stacked with aid: 4. what js should i write for that?
推荐答案
您可以遍历数组的元素,对每个元素进行测试(如果辅助为4的话):
You could loop over the elements of your array, testing, for each one, if its aid is 4 :
var list = [{"aid":"1","atitle":"Ameya R. Kadam"},
{"aid":"2","atitle":"Amritpal Singh"},
{"aid":"3","atitle":"Anwar Syed"},
{"aid":"4","atitle":"Aratrika"},
{"aid":"5","atitle":"Bharti Nagpal"}
];
var length = list.length;
var i;
for (i=0 ; i<length ; i++) {
if (list[i].aid == 4) {
alert(list[i].atitle);
break; // Once the element is found, no need to keep looping
}
}
将使用"Aratrika
"发出警报
这篇关于JSON元素选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!