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 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
随机推荐
- [TJOI2007]书架
题目 网上搜 分析 我们可以认为插入一本书是在树中第 \(k\) 的位置进行插入操作 其中 \(k\) 为这本放入书架后的位置 考虑 \(fhq-treap\) 实现 我们将书编号为 \([0,n-1 ...
- Java 文本检索神器 "正则表达式"
Java 文本检索神器 "正则表达式" 每博一文案 在我们短促而又漫长的一生中,我们在苦苦地寻找人生的幸福,可幸福往往又与我们失之交臂, 当我们为此而耗尽宝贵的.青春年华,皱纹也悄 ...
- bash 和 zsh 中while循环的方式
bash: while true; do ./a.out; done zsh: while true; do ./a.out;
- vue项目中禁用浏览器缓存配置案例
项目发布新版本,部署线上后用户浏览器需要清理缓存 1.public文件夹中修改 index.html文件meta配置 <meta http-equiv="Cache-Control&q ...
- 同一个tomcat的项目跳转
- (app笔记)如何执行monkey命令测试稳定性
1.monkeyMonkey 就是SDK中附带的一个工具,向系统发送伪随机的用户事件流,为了测试软件的稳定性.健壮性,验证app是否出现ANR or Crush 操作: 2. adb devices ...
- 蓝牙mesh组网实践(手机配网例程改低功耗)
目录 在22年7月版本的CH583EVT更新之后,582芯片的adv_vendor_self_provision_with_peripheral例程,适配了wch mesh手机app,支持了OTA,成 ...
- elementui树状结构添加右键点击事件
<el-tree :highlight-current="highlight" :data="folderList" :props="defau ...
- Kubernetes--Pod节点选择器nodeSelector(标签)
Pod节点选择器是标签及标签选择器的一种应用,它能够让Pod对象基于集群中工作节点的标签来挑选倾向运行的目标节点. Kubernetes的kube-scheduler守护进程负责在各工作节点中基于系统 ...
- python multiprocessing调用cython openmp方法需要采用spawn方式
先说结论,在Linux系统,如果python multiprocessing要调用的cython的方法中包含了多线程,比如openmp C code,必须手动设定spawn方式产生多进程. 更多的细节 ...