ES6 数组 some() 和 every() 方法详解与对比

作者:青柠

时间:2026-10-27

浏览:133

本文详细解析ES6中数组的some()与every()方法。some()在至少一个元素满足条件时返回true(一真即真),every()要求所有元素均满足条件才返回true(一假即假)。文章通过代码示例对比两者差异,并简述forEach、map、filter等常用数组方法,帮助开发者准确选择遍历策略。

文章目录

ES6 数组some()和every()使用

从字面含义来看,some意为“部分”,every意为“全部”。因此,some()方法只要数组中有一个元素满足测试条件,就会返回true;相反,every()方法必须所有元素都满足测试条件才会返回true,只要有一个元素不满足(返回false),整个方法就会返回false。

every()和some()的主要目的是确定数组成员是否满足指定的测试条件。

  • every:一假即假
  • some:一真即真
/** 
 * 计算对象数组中每个电脑的扣件系统是否可用,大于16位操作系统表示可用,否则不可用
*/
var computers = [
    {name:"Apple",ram:8},
    {name:"IBM",ram:4},
    {name:"Acer",ram:32},
];
 var result= computers.every(function(computer){
   return computer.ram > 16
})
console.log(result)//false;
var some = computers.some(function(computer){
   return computer.ram > 16
})
console.log(some)//true;
/**
 * 假定有一个注册页面,判断所有Input内容的长度是否大于0
 * 
 */
function Field(value){
    this.value = value
}
// 在原型上定义方法
Field.prototype.validate = function(){
    return this.value.length > 0;
}
var username = new Field('2131');
var telephone  = new Field('8888888888888')
console.log(username.validate() && telephone.validate())//true
 
 
//二`:
var username = new Field('2131');
var telephone  = new Field('8888888888888')
let password  = new Field('');
//console.log(username.validate() && telephone.validate())//只要一个为空就为false
// 简化方式
var fields = [username, telephone,password];
console.log(fields)
var formIsValid = fields.every(function(field){
    return field.validate()
});
console.log(formIsValid)
 
if(formIsValid){
    //注册成功
}else{
    //给用户一个错误提醒
}

ES6数组新增方法

ES6数组新增的一些常用的方法

  • forEach
  • map
  • filter
  • some
  • every
  • find
  • findIndex
  • findLast
  • findLastIndex
  • reduce

以上这些方法,用法都一样,效果不同

arr.方法名((item,index,arr)=>{
})

1. forEach

此方法是用来代替 for 循环遍历数组

let arr=[1,2,3,4];
arr.forEach(function(value,index,arr){
//在这里进行相关操作
})

2.map

返回值是一个新的 数组,数组长度和原数组相同,数组中的值,就是函数中的返回值。

let potatos = [
  { id: '1001', weight: 50 },
  { id: '1002', weight: 80 },
  { id: '1003', weight: 120 },
  { id: '1004', weight: 40 },
  { id: '1005', weight: 110 },
  { id: '1006', weight: 60 }
]
  let w = potatos.map(function(potato) {
        return potato.weight
    })
 //在这里,potato.weight就是函数的返回值

3.filter

此方法是依次拿出数组中的元素,返回符合要求的元素。返回值是一个新的数组,数组中的值是符合条件的值,而这个条件是函数的返回值。

let potatos = [
  { id: '1001', weight: 50 },
  { id: '1002', weight: 80 },
  { id: '1003', weight: 120 },
  { id: '1004', weight: 40 },
  { id: '1005', weight: 110 },
  { id: '1006', weight: 60 }
]
let bigPotatos=potatos.filter(potato=>potato.weight>=100)
//potato.weight>=100 就是返回值,为布尔值,如果为true,则当前遍历到potato就会作为新数组中的值

4.some

此方法返回值是布尔值,判断数组中是否有符合条件的值,而这个条件是函数的返回值

let potatos = [
  { id: '1001', weight: 50 },
  { id: '1002', weight: 80 },
  { id: '1003', weight: 120 },
  { id: '1004', weight: 40 },
  { id: '1005', weight: 110 },
  { id: '1006', weight: 60 }
]

let hasBig = potatos.some(potato => potato.weight >= 100)
// 只要返回值为true,则内部停止遍历,some返回值true,如果每次都返回false,则some返回值为false

5.every

返回值是布尔值,判断数组中的值是否都符合条件,如果是则返回true,有一个不符合则返回false

let potatos = [
  { id: '1001', weight: 50 },
  { id: '1002', weight: 80 },
  { id: '1003', weight: 120 },
  { id: '1004', weight: 40 },
  { id: '1005', weight: 110 },
  { id: '1006', weight: 60 }
]

let hasBig = potatos.every(potato => potato.weight >= 100)
// 只要所有返回值为true,则every返回true,如果由一次返回false,则every返回值为false

6.find 、findLast

返回值为符合条件的对应的那个值

后者从后往前遍历

let potatos = [
  { id: '1001', weight: 50 },
  { id: '1002', weight: 80 },
  { id: '1003', weight: 120 },
  { id: '1004', weight: 40 },
  { id: '1005', weight: 110 },
  { id: '1006', weight: 60 }
]

let bigPotato = potatos.find(potato => potato.weight >= 100) 
// 得到第一个符合条件的数据,返回给变量

7.findIndex 、findLastIndex

返回值为符合条件的对应的那个值的下标

后者从后往前遍历

let potatos = [
  { id: '1001', weight: 50 },
  { id: '1002', weight: 80 },
  { id: '1003', weight: 120 },
  { id: '1004', weight: 40 },
  { id: '1005', weight: 110 },
  { id: '1006', weight: 60 }
]

let bigPotato = potatos.findIndex(potato => potato.weight >= 100) 
// 得到第一个符合条件的数据的下标,返回给变量

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持本站。

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章