数组中的方法

filter

filter方法可以过滤符合条件的数值,返回一个新数组,可以利用filter和indexOf进行数组去重操作(indexOf返回的是该数组内的值第一次出现的索引,若无该值返回-1)

var arr =[1,2,3,4,4,2] ;

arr = arr.filter((item,index)=>{(item, index) => <br>  arr.indexOf(item) === index<br>})//arr--[1,2,3,4]

some

数组.forEach方法会循环数组,且会进行一个完整的循环,无法被终止,浪费性能

数组.some方法在找到数据后就可以使用return true终止some

const arr =[1,2,3,4] ;

arr.some((item,index)=>{

if(item==="3"){

console.log(index);

retuen true

}

})

every

数组.every常用来判断是否全选,只有条件全部满足才返回true

const arr=[

{id:1,name:'zs',state:true},

{id:2,name:'zs',state:false},

{id:3,name:'zs',state:true},

]

const result = arr.every(item=>item.state)

reduce

数组.reduce是一个函数循环累加器

const arr=[

{id:1,name:'西瓜',state:true,price:10,count:1},

{id:2,name:'榴莲',state:true,price:20,count:2},

{id:3,name:'草莓',state:true,price:30,count:3},

]

//累加选中的水果价格

//普通做法

let sum = 0;

arr.filter(item=>item.state).forEach(item=>{

sum += item.price*item.count

})

//使用reduce,不用在外面定义sum,直接在方法内定义

//arr.filter(item=>item.state).reduce((结果,item)=>{},初始值)

arr.filter(item=>item.state).reduce((sum,item)=>{

return sum += item.price*item.count

},0)

thirty的更多相关文章

  1. 第一册:lesson thirty nine.

    原文: Don't drop it! A:What are you going to do with that vase,Penny? B:I am going to put it on the ta ...

  2. 第一册:lesson thirty seven。

    原文: Making a bookcase. A:You are working hard,George. What are you doing . B:I am making a bookcase. ...

  3. 第一册:lesson thirty five。

    原文: Our village . This is a photograph of our village. Our village is in  a valley. It is between to ...

  4. 第一册:lesson thirty three。

    原文:A fine day. It is a fine day today. There are some clouds in the sky. But the sun is shining. Mr. ...

  5. 第一册:lesson thirty one。

    原文:Where is Sally? A:Where is .. B? B:She is in the garden,A. A:What's she doing? B:She is sitting u ...

  6. python's thirty day for me 异常处理

    ---恢复内容开始--- 程序的异常:报错之后程序终止. 异常处理搭配使用: l = ['创建老师','创建学校'] while True: try: for num,item in enumerat ...

  7. [Erlang 0119] Erlang OTP 源码阅读指引

      上周Erlang讨论群里面提到lists的++实现,争论大多基于猜测,其实打开代码看一下就都明了.贴出代码截图后有同学问这代码是哪里找的?   "代码去哪里找?",关于Erla ...

  8. USACO . Friday the Thirteenth

    Friday the Thirteenth Is Friday the 13th really an unusual event? That is, does the 13th of the mont ...

  9. [LeetCode] Integer to English Words 整数转为英文单词

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  10. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

随机推荐

  1. 安卓ro.serialno产生的整个流程

    前言: 关于ro.serialno这个属性,相信大家都不陌生了,应用层的Build.getSerial(),Build.SERIAL等均是直接或间接的获取了这个属性值.接下来从boot到系统应用,小小 ...

  2. ChatGpt聊天API使用

    昨天ChatGpt发布了聊天API,新增了两个模型,目前还是测试阶段 gpt-3.5-turbo 功能强大的GPT-3.5模型,专门针对聊天做了优化 gpt-3.5-turbo-0301 此模型只支持 ...

  3. spring 特性

    1.Aware系列接口 spring 6.0提供了一系列的Aware接口,方便我们在Bean加载时获取信息 如 @Service public class study implements BeanN ...

  4. 异常:java.sql.SQLException: HOUR_OF_DAY: 0 -> 1解决

    问题:Error attempting to get column 'xxTime' from result set. Cause: java.sql.SQLException: HOUR_OF_DA ...

  5. 码云或github的"免费服务器"

    目录 一. 码云及工具介绍 二. 操作步骤 (1) 创建vue-cli项目 (2) 码云创建仓库 (3) 修改并提交项目到码云仓库 (4) 运行项目 (5) 注意点 三. 尾声 对于学生党来说,买个服 ...

  6. Cisco ASA防火墙恢复密码和基本配置

    Cisco ASA密码恢复 一.思路 Cisco ASA防火墙密码恢复,与路由器相似 修改寄存器的值,绕过startup-config配置文件 重新修改密码 恢复修改寄存器的值,保存配置 二.操作步骤 ...

  7. Gitlab CICD

    简介 最近公司要求要通过Gitlab CICD做一个项目的自动打包,我之前也没接触过,所以只能现学现卖.不过说实话,让我这个连大学英语四级都没过的人看Gitlab官网的纯英文教程,我真的是脑阔昏,于是 ...

  8. maven上传jar包或pom文件到远程仓库

    一. 步骤 有时候,项目中打好的jar包或pom文件需要上传到远程仓库,步骤总结如下: 安装好maven,网上有很多教程,默认已安装 工程中的settings.xml增加相应的server账号密码: ...

  9. antd切换主题

    { "name": "my-react-app", "version": "0.1.0", "private& ...

  10. 查电脑并修改IP地址,你晓得吗?

    查电脑并修改IP地址,你晓得吗?   好记性不如烂笔头,古人的话,浅显却好有深意,越品越有味道.   每次都会忘记怎么查电脑IP,那么今天就写下来吧! 方法一:通过命令行查询IP地址   快捷键Win ...