首页 > 开发 > JS > 正文

js实现for循环跳过undefined值示例

2024-05-06 16:52:59
字体:
来源:转载
供稿:网友

本文实例讲述了js实现for循环跳过undefined值的方法。分享给大家供大家参考,具体如下:

<script>var narr=['a','b','c','d','e'];console.log(narr);for (var j=0;j<narr.length;j++){  console.log(j+'=>'+narr[j]);};console.log(narr);for (var j=0;j<narr.length;j++){  console.log(j+'=>'+narr[j]);  if (narr[j] == 'c'){     //narr.splice(j,1);     delete narr[j];  };};console.log(narr);for (var j in narr){  console.log(j+'=>'+narr[j]);};/*(5) ["a", "b", "c", "d", "e"]0: "a"1: "b"3: "d"4: "e"length: 5__proto__: Array(0)h23.js:4 0=>ah23.js:4 1=>bh23.js:4 2=>ch23.js:4 3=>dh23.js:4 4=>eh23.js:7 (5) ["a", "b", "c", "d", "e"]0: "a"1: "b"3: "d"4: "e"length: 5__proto__: Array(0)h23.js:9 0=>ah23.js:9 1=>bh23.js:9 2=>ch23.js:9 3=>dh23.js:9 4=>eh23.js:15 (5) ["a", "b", empty, "d", "e"]0: "a"1: "b"3: "d"4: "e"length: 5__proto__: Array(0)h23.js:17 0=>ah23.js:17 1=>bh23.js:17 3=>dh23.js:17 4=>e*/</script>

运行效果:

js,for循环,undefined

这种方式遍历跳过undefined

希望本文所述对大家JavaScript程序设计有所帮助。


注:相关教程知识阅读请移步到JavaScript/Ajax教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表