thirty
数组中的方法
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的更多相关文章
- 第一册: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 ...
- 第一册:lesson thirty seven。
原文: Making a bookcase. A:You are working hard,George. What are you doing . B:I am making a bookcase. ...
- 第一册:lesson thirty five。
原文: Our village . This is a photograph of our village. Our village is in a valley. It is between to ...
- 第一册: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. ...
- 第一册: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 ...
- python's thirty day for me 异常处理
---恢复内容开始--- 程序的异常:报错之后程序终止. 异常处理搭配使用: l = ['创建老师','创建学校'] while True: try: for num,item in enumerat ...
- [Erlang 0119] Erlang OTP 源码阅读指引
上周Erlang讨论群里面提到lists的++实现,争论大多基于猜测,其实打开代码看一下就都明了.贴出代码截图后有同学问这代码是哪里找的? "代码去哪里找?",关于Erla ...
- USACO . Friday the Thirteenth
Friday the Thirteenth Is Friday the 13th really an unusual event? That is, does the 13th of the mont ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
随机推荐
- rin和快速迭代
链接:https://ac.nowcoder.com/acm/contest/3002/E rin最近喜欢上了数论. 然而数论实在太复杂了,她只能研究一些简单的问题. 这天,她在研究正整数因子个数的时 ...
- WHAT IS PPM Encoder ?
About PPM Encoder The PPM encoder allows to encode up to 8 PWM (pulse width modulated) signals into ...
- 理论+实战,详解Sharding Sphere-jdbc
摘要:Apache ShardingSphere 是一款分布式的数据库生态系统,它包含两大产品:ShardingSphere-Proxy和ShardingSphere-JDBC. 本文分享自华为云社区 ...
- Spring构造函数
Spring构造函数 目录 Spring构造函数 一.分析说明 2.1.一个构造函数 java版本 xml配置版 2.2.多个构造函数 源码思路 为什么分越少优先级越高? 一.分析说明 Spring中 ...
- window C盘满了/文件夹太长怎么移动
通过以下命令移动文件夹 robocopy C:\Users\test\ E:\test\ /move /e C盘满了怎么办? 文件移动到其他盘,通过软连接访问 mklink /d "C:\U ...
- unity通过隐藏layer隐藏组件
设置组件layer 修改组件layer为需要隐藏的layer 隐藏组件 修改layer为隐藏状态 CenterCam.GetComponent<Camera>().cullingMask ...
- 464. 我能赢吗 (Medium)
问题描述 464. 我能赢吗 (Medium) 在 "100 game" 这个游戏中,两名玩家轮流选择从 1 到 10 的任意整数,累计整数和,先使得累计整数和 达到或超过 100 ...
- celery+redis的使用(异步任务、定时任务)
目录 celery理解 安装celery+redis 异步任务使用 1.基础使用 新建task.py文件 在项目文件目录下执行python交互式编程 在项目文件目录下创建worker消费任务 2.使用 ...
- [UnityShader]unity中2D Sprite显示阴影和接受阴影
首先是效果 要让2D显示阴影,首先假设知道Unity阴影来源(shader中的ShadowCaster) 最简单的方法是,首先从官网下载Sprite-Default.Shader,查看源码 然后复制粘 ...
- C语言联合体(共用体)使用方法及大小计算
作者的话 本文介绍联合体的定义.如何使用联合体,包括联合体的声明.联合体变量创建.联合体内存使用,以及联合体大小的计算,最后附上用联合体判断当前环境是大端还是小端的方法. 联合体的定义 联合体,又叫共 ...