简体   繁体   English

为什么我们在javascript for循环中使用-1?

[英]why we use -1 in javascript for loop?

why we use -1 in javascript for loop 为什么我们在javascript中使用-1进行循环

the example code here 这里的示例代码

 var arr = [1,2,2,3,4,5,5,5,6,7,7,8,9,10,10]; function squash(arr){ var tmp = []; for(var i = 0; i < arr.length; i++){ if(tmp.indexOf(arr[i]) == -1){ tmp.push(arr[i]); } } return tmp; } console.log(squash(arr)); 

The indexOf function returns -1 if the item is not found in the desired array. 所述indexOf函数返回-1如果该项目不是所需的阵列中找到。

 document.write([1, 2, 3].indexOf(1)+" "); //Exists document.write([1, 2, 3].indexOf(0)); //Does not exist 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM