数组循环方法

254 阅读3分钟

通常在工作中需要循环时我会用for循环,但是也有几个很方便的数组循环方法

forEch()

forEch的功能基本和for一样可以理解成代替了for循环,而forEc参数有两个;

参数:

arr.forEach(function, thisValue)

参数 可选值 描述 是否必选
function 回调函数 数组中每个元素需要调用的函数,函数参数 true
thisValue --- 传递给函数的值一般用 "this" 值。如果这个参数为空, "undefined" 会传递给 "this" 值 false

例:

let arr = ['富强', '民主', '文明', '和谐', '自由', '平等', '公正', '法治', '爱国', '敬业', '诚信', '友善',]
arr.forEach((item, index, arr) => {
    console.log(item, index, arr)
})

其实第二个参数也很少用到,意思就是当你在forEch中使用this是这个this只的是谁,我理解的就是改变this的指向;因为用到的几率特别小所以想了解的可以去 JavaScript菜鸟教程看一下

map()

参数

map的参数和forEch是一样的

参数 可选值 描述 是否必选
function 回调函数 数组中每个元素需要调用的函数,函数参数 true
thisValue --- 传递给函数的值一般用 "this" 值。如果这个参数为空, "undefined" 会传递给 "this" 值 false

map()方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值,不会对空数组进行检测,不会改变原始数组正常情况下是要配合return;

例:

    let arr =[
    	{title:'富强', Hot:false},
    	{title:'民主', Hot:true},
    	{title:'文明', Hot:false},
    	{title:'和谐', Hot:true},
    	{title:'自由', Hot:false}
    ];
    
    arr.map((item, index, arr)=>{
        console.log(item, index, arr)
    })

上面也说了,正常情况下是要配合return的;

    let arr =[
    	{title:'富强', Hot:false},
    	{title:'民主', Hot:true},
    	{title:'文明', Hot:false},
    	{title:'和谐', Hot:true},
    	{title:'自由', Hot:false}
    ];
    
    let Newarr = arr.map((item, index, arr)=>{
        return item.title
    })
    console.log(Newarr)

返回新数组并且只取title,且不会改变原数组

如果没有return的情况下map相当于for循环

上面也说了map返回的是原始数组元素调用函数处理后的新数组所以...

!!!一定要写return

filter()

( 过滤 ) 简单来说就是过滤掉你想要或者不想要的数据,返回符合条件的元素的数组

    let arr =[
    	{title:'富强', Hot:false},
    	{title:'民主', Hot:true},
    	{title:'文明', Hot:false},
    	{title:'和谐', Hot:true},
    	{title:'自由', Hot:false}
    ];
    //只取Hot为true的
    let Newarr = arr.filter((item, index, arr)=>{
        return item.Hot 
    })
    console.log(Newarr)

同样不改变原始数组;

some()

some() 方法测试数组中是不是至少有1个元素通过了被提供的函数测试。它返回的是一个Boolean类型的值

只要被执行数组中有符合提供的函数测试的就返回true反之为false

    let arr =[
    	{title:'富强', Hot:false},
    	{title:'民主', Hot:false},
    	{title:'文明', Hot:false},
    	{title:'和谐', Hot:false},
    	{title:'自由', Hot:false}
    ];
    
    let Newarr = arr.some((item, index, arr)=>{
    	return item.title == '和谐';
    })
    console.log(Newarr)//true

every()

every()方法用于检测数组所有元素是否都符合指定条件,方法使用指定函数检测数组中的所有元素 如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测,如果所有元素都满足条件,则返回 true

    let arr =[
    	{title:'富强', Hot:false},
    	{title:'民主', Hot:false},
    	{title:'文明', Hot:false},
    	{title:'和谐', Hot:true},
    	{title:'自由', Hot:false}
    ];
    
    let Newarr = arr.every((item, index, arr)=>{
	    return item.Hot == false;
    })
    console.log(Newarr)//false

for of

    let arr =[
    	{title:'富强', Hot:false},
    	{title:'民主', Hot:false},
    	{title:'文明', Hot:false},
    	{title:'和谐', Hot:false},
    	{title:'自由', Hot:false}
    ];

    

    

    

    
    for(let item of arr)
    {
    	console.group("获取每一项");
    	console.log(item);
    	console.groupEnd();
    }

    for(let index of arr.keys())
    {
    	console.group('获取每一项的索引');
    	console.log(index)
    	console.groupEnd();
    }

    for(let val of arr.entries())
    {
    	console.group("获取索引和每一项");
    	console.log(val)
    	console.groupEnd();
    }

    for(let [index,item] of arr.entries())
    {
    	console.group("单独获取索引和每一项")
    	console.log(index)
    	console.log(item)
    	console.groupEnd();
    }

本文章仅以自己防止忘记而记录,不管是我还是其他人,当你看到这篇文章的时候说明你还在从事前端工作,或者在学习前端,所以不管以后遇到什么问题,和挫折,都不要忘记你敲代码的初衷