如何使用jquery迭代数组数组

时间:2015-09-18 14:02:49

标签: jquery

我的阵列: -

var data = [
    [4, "Shailesh ", "Sharma"],
    [5, "Devesh Kumar ", "Neema"],
    [6, "Vardan S ", "Khanna"],
    [208, "Kamlesh ", "Lohiya"],
    [210, "Raj Bahadur ", "Singh"]
]

3 个答案:

答案 0 :(得分:5)

您可以使用jQuery.each()来执行此操作,例如:

var data = [
    [4, "Shailesh ", "Sharma"],
    [5, "Devesh Kumar ", "Neema"],
    [6, "Vardan S ", "Khanna"],
    [208, "Kamlesh ", "Lohiya"],
    [210, "Raj Bahadur ", "Singh"]
]

$.each(data, function (i, n) {
    $.each(n, function (i2, n2) {
        console.log(i2, n2);
    });
});

修改

用于添加选项以从数组中获取数据

var option = '';
$.each(data, function (i, n) {
    option += '<option value="' + n[0] + '">' + n[1] + '</option>';
});
console.log(option);
$('select').append(option);

答案 1 :(得分:1)

var a = [[4,"Shailesh ","Sharma"],[5,"Devesh Kumar ","Neema"],[6,"Vardan S ","Khanna"],[208,"Kamlesh ","Lohiya"],[210,"Raj Bahadur ","Singh"]]

for(var i=0; i<a.length;i++)
{
  for(var j=0; j<a[i].length;j++){
  console.log("value:"+a[i][j]);
  }
}

答案 2 :(得分:0)

在纯JavaScript中:

var data = [
  [4, "Shailesh ", "Sharma"],
  [5, "Devesh Kumar ", "Neema"],
  [6, "Vardan S ", "Khanna"],
  [208, "Kamlesh ", "Lohiya"],
  [210, "Raj Bahadur ", "Singh"]
];

data.forEach(function (a) {
  console.log(a[0] + a[1] + a[2]);
});

我真的没有理由在jQuery中做到这一点。