数组的some方法
1、打开编辑器,新建一个HTML文档,作为示范,然后设立HTML的基本架构。
2、创建JS文件,并且关联HTML文档。
3、let students = ["Peter", "Alice", "Chris", "Baby", "Sherry"];let test1 = students.some(function(title, index){ console.log(index, title); return true;})创建一个数组,并且使用some方法。some遇到true以后就停止循环了,因此只打印了第一个元素。
4、let test1 = students.some(function(title, index){ console.log(index, title); return false;})如果是遇到false就会一直循环下去,知道打印完。
5、return title.indexOf("Alice") > -1;改变一下判断条件,遇到"Alice"以后再停止循环。
6、let test1 = students.some(function(title, index){ console.log(index, title); if(index == 2){ return true; };})我们还可以用if语句来进行判断,这样可以终止循环。
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:71
阅读量:32
阅读量:22
阅读量:82
阅读量:51