在jquery中使用foreach JSON数组

时间:2016-11-12 09:28:54

标签: php jquery arrays json

我有一个以下格式的数组:

[["tag1","tag2","tag3","tag4","tag5","tag6","tag7","Thy","YUI"],["","",0,"jkghjg","hgjhg","kjhkjhg","kjghjkg)","gjhg","jkjkghj"]]

我需要“预测”数组并逐个获取每个值,如:Features,notes ...

当我把它放入$.each()时,我收到了这个错误:

Uncaught TypeError: Cannot use 'in' operator to search for '1103' in ..

我如何得到它?

3 个答案:

答案 0 :(得分:0)

试试这个

var a = [["Features", "Notes", "Code", "Value", "Testing", "BAC", "Total", "Thy", "YUI"], ["", "", 0, "jkghjg", "hgjhg", "kjhkjhg", "kjghjkg)", "gjhg", "jkjkghj"]]


$.each(a, function (i, data) {
    $.each(data, function (j, val) {
        console.log(val);
    });
});

如果您想使用foreach方法

,也可以尝试此操作
Array.prototype.forEach.call(a,function(rm){
  rm.map(function(ed){
     console.log(ed)
  })
})

答案 1 :(得分:0)

你的意思是将数组作为关联JSON对象吗? 试试这个

var a = [["Features", "Notes", "Code", "Value", "Testing", "BAC", "Total", "Thy", "YUI"],
         ["", "", 0, "jkghjg", "hgjhg", "kjhkjhg", "kjghjkg)", "gjhg", "jkjkghj"]]

var json = {}
for (var i in a[0]) {
  var key = a[0][i];
  var val = a[1][i];
  json[key] = val;
}
console.log(json);
console.log(json.Testing);

答案 2 :(得分:0)

var a_data= [["Features","Notes","Code","Value","Testing","BAC","Total","Thy","YUI"],["","",0,"jkghjg","hgjhg","kjhkjhg","kjghjkg)","gjhg","jkjkghj"]]
var output="";
$.each(a_data,function(index,data){
   output+=data.join(",")
})
console.log(output)