广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >ES6数组some()和every()的使用及说明
  • 745
分享到

ES6数组some()和every()的使用及说明

ES6数组使用ES6数组some()ES6数组every() 2023-01-16 12:01:10 745人浏览 独家记忆
摘要

目录es6 数组some()和every()使用ES6数组新增方法1. forEach2.map3.filter4.some5.every6.find 、findLast7.find

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

some 英语翻译为一些,every翻译为所有,每个,所以some方法 只要其中一个为true 就会返回true的,相反,every()方法必须所有都返回true才会返回true,哪怕有一个false,就会返回false;

every()和 some()目的:确定数组的所有成员是否满足指定的测试

  • every:一假即假
  • some:一真即真 

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;

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) 
// 得到第一个符合条件的数据的下标,返回给变量

总结

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

--结束END--

本文标题: ES6数组some()和every()的使用及说明

本文链接: https://www.lsjlt.com/news/177852.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作