数组中的方法

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. JZOJ 2474. 【GDKOI 2021普及组DAY2】我的世界

    题解 这题很明显发现一个点到另一个点,必然最多只有一个进入下界的点和一个出来的点 分类讨论入点和出点的位置 要么都在 \(u->lca\) 或都在 \(lca->v\) 或分别有一个 那就 ...

  2. 抗TNF治疗改变JIA患者PBMC基因表达谱,可预测疗效

    抗TNF治疗改变JIA患者PBMC基因表达谱,可预测疗效 Moorthy LN, et al. ACR 2007. Presentation No:1713. 背景:我们假设儿童期发生的特发性关节炎( ...

  3. CSS:linear-gradient()背景颜色渐变

    css语法 background: linear-gradient(direction,color-stop1,color-stop2,...); direction:用角度值指定渐变的方向(或角度) ...

  4. Redis Stream类型的使用详解

    目录 一.背景 二.redis中Stream类型的特点 三.Stream的结构 四.Stream的命令 1.XADD 往Stream末尾添加消息 1.命令格式 2.举例 2.XRANGE查看Strea ...

  5. mysql中int、bigint、smallint 和 tinyint四种数据类型

    最近在做数据库表设计的时候,对于多种数字的数据类型的选择存在很多顾虑,不是很清楚到底如何选择.总结一下int.bigint.smallint 和 tinyint四种数据类型. bigint:从 -2^ ...

  6. Centos7提示Initial setup of CentOS Linux 7 (core)

    Initial setup of CentOS Linux 7 (core) 1) [x] Creat user 2) [!] License information (no user will be ...

  7. windows搭建minikube环境学习Kubernates

    1.使用powershell下载minikube New-Item -Path 'c:\' -Name 'minikube' -ItemType Directory -Force Invoke-Web ...

  8. P1046 [NOIP2005 普及组] 陶陶摘苹果

    题目描述 陶陶家的院子里有一棵苹果树,每到秋天树上就会结出 10 个苹果.苹果成熟的时候,陶陶就会跑去摘苹果.陶陶有个 30厘米高的板凳,当她不能直接用手摘到苹果的时候,就会踩到板凳上再试试. 现在已 ...

  9. JS语句创建简单表格

    var line=3; var list=3; var table=document.createElement("table"); table.setAttribute(&quo ...

  10. 001.shell-每日练习一文件创建

    001.shell-每日练习一文件创建 0x00.练习要求 在/usr/local/uz654目录下,按照xxxx-xx-xx生成一个文件,如:2023-02-11.log 把磁盘的使用情况写到这个文 ...