[英]How to push values to a multi-dimensional array
我無法弄清楚如何將值推送到動態多維數組。 這是我的代碼:
function compareNumbers(){
var count = $('.finishedRow').length;
var inputtedNums = new Array();
for(var i=0; i<count; i++){
$('.finishedRow').eq(i).find('li').each(function(j){
inputtedNums[i].push($(this).text());
});
}
console.log(inputtedNums);
}
所以說,有,例如,3個finishedRow
選擇器,並且每個finishedRow
選擇器包含4個li
用的值的元素first
, second
, third
, fourth
。 我希望我的inputtedNums
變量看起來像:
inputtedNums = [
["first", "second", "third", "fourth"],
["first", "second", "third", "fourth"],
["first", "second", "third", "fourth"]
]
正如我的代碼現在我得到錯誤: Cannot call method 'push' of undefined
。
我敢肯定我在這里缺少一些基本的東西。
您需要首先初始化每個嵌套數組。
for(var i = 0; i < count; i++) {
inputtedNums[i] = new Array();
$('.finishedRow').eq(i).find('li').each(function(j) {
inputtedNums[i].push($(this).text());
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.