数组中的方法

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. 一文详解ATK Loss论文复现与代码实战

    摘要:该方法的主要思想是使用数值较大的排在前面的梯度进行反向传播,可以认为是一种在线难例挖掘方法,该方法使模型讲注意力放在较难学习的样本上,以此让模型产生更好的效果. 本文分享自华为云社区<AT ...

  2. go并发实战(读书笔记1)

    go并发实战,第一天 大部分本书第一章节是来介绍go语言基础的,其实如果你不是大师,只是一个才起飞的菜鸟,建议不要跳过喔! 为什么不要跳过?因为每个人对语言的认知是不一样的,看看别人是怎么理解一个新事 ...

  3. 运维排查篇 | Linux 连接跟踪表满了怎么处理

    nf_conntrack (在老版本的 Linux 内核中叫 ip_conntrack )是一个内核模块,用于跟踪一个网络连接的状态 一旦内核 netfilter 模块 conntrack 相关参数配 ...

  4. ubuntu 启动脚本变化

    ubuntu-16.10 开始不再使用initd管理系统,改用systemd- 快速看了 systemd 的使用方法,发现改动有点大, 包括用 systemctl 命令来替换了 service 和 c ...

  5. CSS3,线性渐变(适用标题背景)

    .test{ margin:200px auto; height:30px; border:1px #D4D4D4 solid; box-shadow:0 -1px 10px rgba(0,0,0,0 ...

  6. 【博图scl语言】313-2dp

    ①如果 if(***) then *** := ***; end_if; ②循环 for n1:=1 to 50 by 1 do end_for; WHILE #n1 < 54 DO END_W ...

  7. 图 -拓扑 topo

    https://www.cnblogs.com/New-ljx/p/13874648.html 在有向无环图上,用拓扑排序在O(n)的时间内求出最短/长路,是一个不错的选择(也称拓扑的DP). 只需要 ...

  8. 概率生成函数(PGF)简记

    基本搬运自<浅谈生成函数在掷骰子问题上的应用>. 对于定义在非负整数上的离散随机变量 \(X\),级数 \(F(z) = \sum\limits_{i\ge 0} \operatornam ...

  9. BOOK01:《过目不忘的读书法》

    BOOK01:<过目不忘的读书法> 01 区分"信息"和"知识" 一年之后过时的是"信息",十年也不过时的是"知识&q ...

  10. PXE自动安装linux系统

    一.PXE自动安装Linux系统的大致流程 1.环境准备 2.安装所需的必要软件包并开启对应的服务 3.准备安装源 4.准备自动应答文件,并放到可以被访问到的地方 5.配置DHCP服务并启动 6.将必 ...