💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
## for循环 ~~~ for(j = 0; j < arr.length; j++) { } //其它样式的for循环 for(j = 0,len=arr.length; j < len; j++) { } //用临时变量将长度缓存起来,避免重复获取数组长度,性能比for好 for(i = 0; arr[i] != null; i++){ } //比for性能差 ~~~ ## forEach 循环 (性能比普通for弱) ``` arr.forEach(function(i){ }) ``` ## for in (可以遍历对象 ,性能最差) ``` for(j in obj){ } ``` ## map遍历(不如forEach高效) ``` arr.map(function(n){ } ``` ## forof遍历(需要ES6支持) ``` for(let value of arr) { } ``` ![](https://box.kancloud.cn/74ac16749c60e4acc0281572761d47f3_873x1164.png) ## some filter findIndex **some 可以通过 return turn终止循环 ** **filter 把符合条件返回到一个新数组 ** **findIndex 找到对应项的索引 ** ``` del(id) { // 在数字中some 方法如果return true,就会立即终止循环 // this.list.some((item,i)=>{ // if(item.id == id){ // this.list.splice(i,1); // return true; // }; // // console.log(data.list); // }) /* findIndex一个方法查索引 */ var index = this.list.findIndex(item => { if (item.id == id) { return true; } }) console.log(index); this.list.splice(index, 1); }, search(keywords) { var newList = []; this.list.forEach(item=>{ if (item.name.indexOf(keywords) != -1) { newList.push(item) } }) return newList; /* forEach some 可以通过 return turn终止循环 filter 把符合条件返回到一个新数组 findIndex 找到对应项的索引 */ // var newList = this.list.filter(item => { // // if(item.name.indexOf(keywords) != -1) // // 注意:ES6中,为字符串提供了一个新方法,叫做 String.prototype.includes('要包含的字符串') // if (item.name.includes(keywords)) { // return item; // } // }) // return newList; } } ``` [参考](https://dailc.github.io/2016/11/25/baseKnowlenge_javascript_jsarrayGoThrough.html)