数组中的方法

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. Python:Excel自动化实践入门篇 甲【留言点赞领图书门票】

    *以下内容为本人的学习笔记,如需要转载,请声明原文链接微信公众号「englyf」https://mp.weixin.qq.com/s?__biz=MzUxMTgxMzExNQ==&mid=22 ...

  2. Moonraker

    Moonraker 目录 Moonraker 1 信息收集 1.1 端口扫描 1.2 后台目录扫描 1.3 目录分析 1.3.1 /services/ 1.3.2 /svc-inq/salesmoon ...

  3. pdf地址展示成Swiper轮播方式-复制链接

    1.安装vue-pdf插件,swiper插件.clipboard npm install vue-pdf -snpm install swiper -Snpm install clipborad -S ...

  4. ArcGIS for Android 实现地图基本操作

    地图基本操作 1.前期项目准备 1.1. 创建新工程 新建一个空活动项目 选择语言.平台,修改命名等 1.2. 添加ArcGIS SDK build.gradle (Project: <proj ...

  5. IntelliJ IDEA运行项目的时候提示 Command line is too long 错误

    这时候你需要调整运行项目的配置,将 Configuration 中的 Shorten Command Line 修改为 JAR 就可以了.

  6. 如何批量删除office文档属性

    在文件资源管理器界面全选所有office文档, 右键->属性->详细信息->删除属性和个人信息->从此文件中删除以下信息->全选->确定,即可.

  7. 在npm中定义变量

    Node_Dev=Dev 在js文件里可以通过prosess.env获取该变量

  8. 回归分析 3.X 多元线性回归

    多元线性回归模型 参数估计 模型表示 我们先将模型 \[y_{i}=\beta_{0}+\beta_{1} x_{i 1}+\cdots+\beta_{p} x_{i k}+\epsilon_{i}, ...

  9. 十二、21.提交本地代码到Git仓库并推送到码云

    查看分支 运行git add . 把所有修改过后文件添加到暂存区 git commit 把当前所有的代码提交到rights分支 加-m加一个消息 到此所有的功能模块都已经提交到了rights这个分支里 ...

  10. CodeGym自学笔记06——内存寻址和变量

    内存寻址和变量 print() print() 函数用于在屏幕上逐个字符显示文本.当屏幕上某一行没有足够空间时,文本开始在下一行显示. println() 可以使用 println() 函数停止在当前 ...