1、过滤数组 -filter
筛选数组元素,并生成新数组
//过滤出分数为60分以上的数据 <script> const arr = [ {'name':'张三','score':80}, {'name':'张六','score':50}, {'name':'李四','score':90}, {'name':'王五','score':60}, ] const newArr = arr.filter(function(item,index){ return item.score>=60 }) console.log(newArr) </script>
2、迭代数组-map
返回新数组,新数组里面的元素是处理之后的值,经常用于处理数据
<script> //给数据的分数都加上10分 const arr = [ {'name':'张三','score':80}, {'name':'张六','score':50}, {'name':'李四','score':90}, {'name':'王五','score':60}, ] arr.map(function(item,index){ item.score+=10 }) console.log(arr) </script>
3、遍历数组-forEach
不返回,用于不改变值,经常用于查找打印输出值
<script> //遍历数组 const arr = [ {'name':'张三','score':80}, {'name':'张六','score':50}, {'name':'李四','score':90}, {'name':'王五','score':60}, ] arr.forEach(function(item,index){ console.log(item.name) }) </script>
4、累计器-reduce
返回函数累计处理的结果,经常用于求和等
//语法:arr.reduce(function(累计值, 当前元素){}, 起始值) // prev=5初始化值 <script> const arr = [1, 2, 3] const re = arr.reduce((prev, item) => prev + item,5) console.log(re) </script>
标签:map,arr,const,name,reduce,js,item,score,数组 From: https://www.cnblogs.com/ygbh/p/18320999