2017-07-02 166 views
2
<!DOCTYPE html> 
<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<script type="text/javascript"> 
testArray =[[ 'text1', 'text2', 'text3' ], 
    [ 'question', 
     'correct answer', 
     'second answer', 
     'third answer', 
     'fourth answer' ], 
    [ 'question', 
     'correct answer', 
     'second answer', 
     'third answer', 
     'fourth answer', 
     'fifth answer', 
     'sixth answer' ]]; 

</script> 
</head> 

<body> 

<div id = "passage"></div> 
<div id = "question"></div> 
<div id = "answers"></div> 

<script> 

$.each(testArray[1].slice(1), function (i) { 
    $("#answers").append("<p>" + testArray[1][i] + "</p>"); 
}); 

</script> 

</body> 
</html> 

當我跑了這一點,我是通過元素期待最後的每個循環來從testArray[1][1]testArray[1][4]。不過由於某種原因,它只能達到testArray[1][3]。有誰知道問題是什麼?Jquery的每個循環沒有得到最後一個元素

編輯:

我做到這一點,它以某種方式工作了什麼,我想做的事(開始於指數1元,並循環到最後):

$.each(testArray[1].slice(1), function (i) { 
    $("#answers").append("<p>" + testArray[1][i + 1] + "</p>"); 
}); 

不知道如何/爲什麼這工作雖然。

+0

'slice'刪除最後一個元素。你不需要使用切片。只要刪除它,它會起作用。 –

+0

我想使用切片,以刪除第一個元素(索引0),並從第二個元素(索引1)開始循環。 –

+0

刪除第一個元素,您需要使用'shift()'或'splice(0,1)' –

回答

0

這確實很奇怪。我與ES6版本嘗試過了,它工作得很好:

testArray[ 1 ].slice(1).forEach(item => { 
    $("#answers").append("<p>" + item + "</p>"); 
    }); 
-1

編輯

的問題是你的切片陣列傳遞給$。每次,但回調裏面你索引原始數組。

$ .each收到的數組是['正確答案','第二個答案','第三個答案','第四個答案'],但它內部從testArray獲取值,當你真的想要它從數組中'傳入。一種解決方案是隻使用傳入的值作爲回調函數的第二個參數。

testArray =[[ 'text1', 'text2', 'text3' ], 
 
    [ 'question', 
 
     'correct answer', 
 
     'second answer', 
 
     'third answer', 
 
     'fourth answer' ], 
 
    [ 'question', 
 
     'correct answer', 
 
     'second answer', 
 
     'third answer', 
 
     'fourth answer', 
 
     'fifth answer', 
 
     'sixth answer' ]]; 
 

 
$.each(testArray[1].slice(1), function (i, value) { 
 
    $("#answers").append("<p>" + value + "</p>"); 
 
});
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script type="text/javascript"> 
 

 

 
</script> 
 
</head> 
 

 
<body> 
 

 
<div id = "passage"></div> 
 
<div id = "question"></div> 
 
<div id = "answers"></div>

+0

我想用切片去掉第一個元素......我該怎麼做? –

+0

爲你更新了我的答案。 –

-1

只需添加一個簡單的if條件,下面是代碼:

testArray = [ 
 
    ['text1', 'text2', 'text3'], 
 
    ['question', 
 
    'correct answer', 
 
    'second answer', 
 
    'third answer', 
 
    'fourth answer' 
 
    ], 
 
    ['question', 
 
    'correct answer', 
 
    'second answer', 
 
    'third answer', 
 
    'fourth answer', 
 
    'fifth answer', 
 
    'sixth answer' 
 
    ] 
 
]; 
 

 
$.each(testArray[1], function(i) { 
 
    if (i > 0) 
 
    $("#answers").append("<p>" + testArray[1][i] + "</p>"); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="passage"></div> 
 
<div id="question"></div> 
 
<div id="answers"></div>

+0

永久更改陣列,這不是我想要的 –

+0

好吧,以便更改您的問題。其次,努力不應該被低估。 –

+0

增加了一個'if'條件來忽略數組的第一個元素而不影響原來的'array' –

相關問題