dicts我有一个由服务器在 ajax 请求中返回的数组,如下所示:
var obj = [{'id': '111', 'name': 'test1'}, {'id': '1975', 'name': 'test2'}]
Run Code Online (Sandbox Code Playgroud)
当我尝试访问每个字典时:
$.each(obj,function(index,value){
alert(index + " : " + value);
});
Run Code Online (Sandbox Code Playgroud)
我收到错误:
未捕获的类型错误:无法使用 'in' 运算符在 [{'id': '111', 'name': 'test1'}, {'id': '1975', 'name': 'test2 中搜索 '193' '}]
如何访问数组中的每个字典?
您需要一个 $.each() 来存储数组,另一个用于处理字典的元素(顺便说一句,大多数 JS 程序员将其称为“对象”)
$.each(obj,function(index,value){
$.each(value, function(index2, value2) {
alert(index2 + " : " + value2);
});
});
Run Code Online (Sandbox Code Playgroud)