数组中的方法

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. Android:Fragment

    Fragment 概念 把 Activity 中的一段 UI 和逻辑封装到一个 Fragment 中,实现可拔插,减少对 Activity 代码的侵入. Fragment 定义和管理自己的布局,具有自 ...

  2. pytorch学习笔记二之自动差分引擎

    自动差分引擎¶   torch.autograd是 PyTorch 的自动差分引擎,可为神经网络训练提供支持.   1. 背景¶   神经网络(NN)是在某些输入数据上执行的嵌套函数的集合. 这些函数 ...

  3. 【磐河旅行】之酒店API接口对接实录

    1.项目需求概述: 通过对接第三方磐河旅行的酒店API接口实现在我们的APP .微信小程序.H5上可提供用户酒店查询.酒店预订.退订等功能.效果如下图: 2.酒店接口功能拆分 除了酒店静态数据字典(如 ...

  4. android 投屏

    https://blog.csdn.net/aa464971/article/details/83349215

  5. qt_2d画图

    qt中如何画图? 使用Qpainter类进行2D绘画 例如以下代码进行直线的绘制 QPainter painter(this);painter.setPen(Qt::red);painter.draw ...

  6. Go指南:方法和接口

    方法与指针重定向 带指针参数的函数必须接受一个指针: func ScaleFunc(v *Vertex, f float64) { v.X = v.X * f v.Y = v.Y * f} 以指针为接 ...

  7. c++的double转string(转)

    原文地址:https://www.cnblogs.com/finallyliuyu/p/1810071.html c++中double转换成string型(浮点数的格式化)(转)   在日常编程中-- ...

  8. Chrome浏览器崩溃

    1.使用Win+R打开运行对话框,输入regedit,点击确定打开注册表: 2.找到"HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome&q ...

  9. 52道常见Python面试题,你都看过了吗?(转发供参考学习)

    https://blog.csdn.net/xiaohei3ge/article/details/88080284?utm_medium=distribute.pc_relevant.none-tas ...

  10. linux命令行大量零碎练习习题集-打包未整理

    linux命令行大量零碎练习习题集-打包未整理最近看完了一本linux书籍,想着做题来检测和巩固一下,于是打算去买本linux命令集习题去做做.但是没有找到相关的书籍,于是只能在网上随便找找.但是很多 ...