2015-09-18 66 views
1
遍歷數組的數組

我的數組: -如何使用jQuery

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

嗨@parth歡迎來到SO :-)你可以添加你已經嘗試過的代碼嗎?在這裏,我們在這裏幫助您使用已有的代碼。 – BMac

回答

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); 
+0

這是給我一個單一的字符。我需要雙引號之間的整個值。 –

+0

@parthbhorania:可以提供預期結果的例子嗎? –

+0

我要在「」中使用此值。 –

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]); 
    } 
} 
+0

正確 - 但不使用JQuery :-) – Stumblor

+0

@Stumblor同意! – Akki619

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的。