跳到主要内容

js数组遍历forEach、map、filter、find、every、some、reduce对比

管

管理员

前端

2019年06月28日 1,933 次阅读
本页目录
可以参考一下:
https://blog.csdn.net/weixin_37620905/article/details/82953845  
https://www.cnblogs.com/jiebba/p/6514067.html

*ES6语法:

Opera 11+ 

Firefox 3.6+   

Safari 5+   

Chrome 8+

Internet Explorer 9+支持。可以通过babel转意支持低版本浏览器。

 

forEach()
ES6新增的一种循环

案例1打印数组中所有对象

numbers=[1,2,3,4,5]
numbers.forEach(number => {
    console.log(number)  //1 2 3 4 5
});
案例2将数组中所有数字相加(这里用了函数抽离的方式)

numbers=[1,2,3,4,5]
var sum=0
function add(num){   
    sum+=num
}
numbers.forEach(add)
console.log(sum)    //15
 

map()
原数组被“映射”成对应新数组,返回一个新数组

案例1将原数组的每个数字都*2

var numbers=[1,2,3,4,5]
var doublnumbers=numbers.map(number=>{
    return number*2
})
console.log(doublnumbers)  //[2,4,6,8,10]
案例2将A对象数组中某个属性存到B数组中

var building=[
    {name:'the Great Wall',location:'BeiJing'},
    {name:'Eiffel Tower',location:'Paris '}
]
var citys=building.map(item=>{
    return item.location
})
console.log(citys)  //["BeiJing", "Paris "]
 

filter()
filter为“过滤”。数组filter后,返回过滤后的新数组

案例1假定有一个对象数组A,获取数组中指定类型的对象放到B数组中

var products = [
    {name:"cucumber",type:"vegetable"},
    {name:"banana",type:"fruit"},
    {name:"celery",type:"vegetable"},
    {name:"orange",type:"fruit"}
  ]; 
  var filtered = products.filter((product)=>{
    return product.type === "vegetable";
  })
  console.log(filtered)  //[{name:"cucumber",type:"vegetable"},{name:"celery",type:"vegetable"}]
案例2假定有两个数组(A,B),根据A中id值,过滤掉B数组不等于A中id的数据

var post = {id:4,title:"Javascript"};
var comments = [
    {postId:4,content:"Angular4"},
    {postId:2,content:"Vue.js"},
    {postId:3,content:"Node.js"},
    {postId:4,content:"React.js"}   
 ];
 function commentsForPost(post,comments){
    return comments.filter(function(comment){
      return comment.postId === post.id;
    })
 }
 
 console.log(commentsForPost(post,comments));  //[ {postId:4,content:"Angular4"},{postId:4,content:"React.js"}]
 

find()
在数组中找到符合要求的对象 和filter的区别就是找到符合要求的对象就停止寻找了,返回的是一个对象,而filter返回的是所有符合要求的对象组成的新数组

案例1假定有一个对象数组(A),找到符合条件的对象

var users = [
    {name:"Jill",id:1},
    {name:"Alex",id:2},
    {name:"Bill",id:3},
    {name:"Alex",id:4}
   ];
   user = users.find(function(user){
    return user.name === "Alex";
  })
 console.log(user); //{name: "Alex", id: 2}
案例2假定有两个数组(A,B),根据A中id值,找到B数组等于A中id的数据

var post = {id:4,title:"Javascript"};
 var comments = [
    {postId:4,content:"Angular4"},
    {postId:2,content:"Vue.js"},
    {postId:3,content:"Node.js"},
    {postId:4,content:"React.js"}   
 ];
 function commentsForPost(post,comments){
    return comments.find(function(comment){
      return comment.postId === post.id;
    })
 }
 
 console.log(commentsForPost(post,comments));  // {postId:4,content:"Angular4"}
 

every()  some()
every 若目标数组中每一个对象都符合条件则返回true,否则返回false

some 若目标数组中有一个或一个以上的对象符合条件的返回true,否则返回false

案例

var users = [
    {name:"Jill",age:10},
    {name:"Alex",age:18},
    {name:"Bill",age:20},
    {name:"Tony",age:24}
   ];
 
   var isAdult_every=users.every(user=>{
      return user.age>18;
   })
 
   var isAdult_some=users.some(user=>{
    return user.age>18;
 })
 
   console.log(isAdult_every)  //false
   console.log(isAdult_some)  //true
 

reduce()
常用于叠加,可以代替forEach等

案例计算数组中所有值的总和

var numbers = [1,2,3,4,5];
   var sumValue = numbers.reduce(function(sum,number2){  //第一个参数为叠加总值,需要初始化,第二个参数是当前项
    return sum + number2;
  },0);    //sum的初始化
  console.log(sumValue);   //15
 

 

Related

相关文章

同分类文章 →
2025年06月10日 前端

gemini-2.5-pro-0605 按图生成html效果测试

Gemini 最新推出的 2.5 版本真的有点东西。之前用过不少 AI 辅助写代码的工具,但这次它的“图生 HTML”功能确实让我眼前一亮——上传一张设计图,它不仅能快速识别布局和元素,还能生成结构清晰、样

2025年03月14日 前端

css flex-1和overflow-hidden结合使用详解:弹性布局与内容溢出控制

在 Flexbox 布局中,flex: 1 和 overflow: hidden 是两个常用的属性,它们组合使用可以有效地控制子元素的尺寸和内容显示。flex: 1 的作用:当子元素设置 flex: 1 时,它会根据 Flex 容器的主轴方向,自动伸缩以填充剩余的可用

2025年03月13日 前端

Vue 3中实现处理后的v-model双向绑定

在Vue 3中,当你需要对v-model传入的值进行处理后再展示或使用时,可以使用计算属性的getter和setter来实现这种处理过程的双向绑定。 实现步骤 接收父组件传递的v-model值 创建计算属性,在getter中处理展示值,在setter中处

WeChat

扫码加我微信

微信二维码