这是一个快速的jsfiddle,我提出了一个更好的例子我的问题.
function gi(id){return document.getElementById(id)}
a= [1,5,1,2,3,5,3,4,3,4,3,1,3,6,7,752,23]
for(i=0; i < a.length; i++){
/*
if (WHAT AND WHAT){ What do I add here to know that the last value in the array was used? (For this example, it's the number: 23. Without doing IF==23.
}
*/
gi('test').innerHTML+=''+a[i]+' <br>';
}
Run Code Online (Sandbox Code Playgroud)
(该代码也可在https://jsfiddle.net/qffpcxze/1/获得)
所以,该数组中的最后一个值是23,但是我怎么知道最后一个值是循环内部循环的呢?(如果IF X == 23有意义的话,不检查简单但动态).
编写一个if比较数组长度的语句i
if(a.length - 1 === i) {
console.log('loop ends');
}
Run Code Online (Sandbox Code Playgroud)
或者你可以使用三元组
(a.length - 1 === i) ? console.log('Loop ends') : '';
Run Code Online (Sandbox Code Playgroud)
还要注意,- 1因为数组索引从而开始使用0并且从这里返回计数长度1以便比较数组和我们否定的长度-1.
| 归档时间: |
|
| 查看次数: |
6953 次 |
| 最近记录: |