javascript利用map,every,filter,some,reduce,sort对数组进行最优化处理
案例:
var scoresTable=[
{id:11,name:"小张",score:80},
{id:22,name:"小王",score:95},
{id:33,name:"小李",score:50},
{id:44,name:"小刘",score:65},
{id:55,name:"小徐",score:84}
]
1、快速获取最高score值(采用map,Max.sum和apply)
var scores=scoresTable.map(function(item){return item.score});
var maxScore=Math.max.apply(this,scores)
console.log(maxScore)
2、是否包含不及格学生(some)
var hasFail=scoresTable.some(function(item){return item.score<60;})
console.log(hasFail)
3、求平均分(reduce)
var averageScore=scoresTable.reduce(function(a,b){return a + b.score;},0)/scoresTable.length;
console.log(averageScore)
4、按成绩从高到低排序(sort)
var score=scoresTable.sort(function(a,b){return a.score<b.score;})
console.log(score)
5、找出不及格学生(filter)
var failStudents=scoresTable.filter(function(item){return item.score<60;})
console.log(failStudents)
6、是否所有学生的学号都是11的倍数(every)
var t=scoresTable.every(function(item){return item.id%11==0;})
console.log(t)
javascript利用map,every,filter,some,reduce,sort对数组进行最优化处理的更多相关文章
- JavaScript Array -->map()、filter()、reduce()、forEach()函数的使用
题目: 1.得到 3000 到 3500 之内工资的人. 2.增加一个年龄的字段,并且计算其年龄. 3.打印出每个人的所在城市 4.计算所有人的工资的总和. 测试数据: function getDat ...
- Swift函数编程之Map、Filter、Reduce
在Swift语言中使用Map.Filter.Reduce对Array.Dictionary等集合类型(collection type)进行操作可能对一部分人来说还不是那么的习惯.对于没有接触过函数式编 ...
- ES6 数组遍历方法的实战用法总结(forEach,every,some,map,filter,reduce,reduceRight,indexOf,lastIndexOf)
目录 forEach every some map filter reduce && reduceRight indexOf lastIndexOf 前言 ES6原生语法中提供了非常多 ...
- C#数组的Map、Filter、Reduce操作
在Javascript.Python等语言里,Map.Filter和Reduce是数组的常用方法,可以让你在实现一些数组操作时告别循环,具有很高的实用价值.它们三个的意义大家应该都清楚,有一个十分形象 ...
- map、filter、reduce、lambda
一.map.filter.reduce map(fuction , iterable) 映射 对可迭代对象中的每一项,使用函数去改变 filter(function, iterable) 过滤 可迭代 ...
- 辅助函数和高阶函数 map、filter、reduce
辅助函数和高阶函数 map.filter.reduce: 一.辅助函数:(1-1)响应式函数 (数组更新检测): push() pop() shift() unshift() ...
- python之map、filter、reduce、lambda函数 转
python之map.filter.reduce.lambda函数 转 http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...
- Python 之map、filter、reduce
MAP 1.Python中的map().filter().reduce() 这三个是应用于序列的内置函数,这个序列包括list.tuple.str. 格式: 1>map(func,swq1[,s ...
- python的map,filter,reduce学习
python2,python3中map,filter,reduce区别: 1,在python2 中,map,filter,reduce函数是直接输出结果. 2,在python3中做了些修改,输出前需要 ...
随机推荐
- How to fix Column 'InvariantName' is constrained to be unique 解决办法!
Introduction When you build a web project that uses Enterprise Library Community for the Application ...
- QUIC简要
QUIC.即Quick UDP Internet Connection,类似于SPDY,相同也是由Google公司在现有已存协议之上进行了扩展设计,而旨在降低网络延迟.之前我曾介绍过SPDY的相关信息 ...
- Intel® Ethernet Connection I217-V 网卡驱动(win10 ,2012)
https://downloadcenter.intel.com/zh-cn/download/25016/-Windows-10 上面是win10 的驱动 win8.1 https://downl ...
- php 无错误提示 的解决方法
问:我在win7安装了PHP,浏览器是IE9.我代码写错了,浏览器一点错误提示都没有,一片空白.如果写对了,就能正常运行显示出来.请问这是怎么回事,应该怎么弄?你们两个的方法都试过,但都没有提示(注: ...
- MySQL存储过程:用户授权量
写这些脚本需求放缓的调查记录到数据库,方便观看. 1. 因为默认mysql.slow_log表使用csv数据引擎,该数据不支持指数,因此,有必要改变MyISAM发动机.和query_time字段索引, ...
- 达到HTTP合约Get、Post和文件上传功能——采用WinHttp介面
于<采用WinHttp实现HTTP协议Get.Post和文件上传功能>一文中,我已经比較具体地解说了怎样使用WinHttp接口实现各种协议. 在近期的代码梳理中,我认为Post和文件上传模 ...
- Android4.0 Design之UI设计缺陷1
我想成为Android卓越发展project联赛,不知道Android它如何设计规则,Android4.0谷歌公司的问世后Android一系列的设计原则,程序猿规范,不要盲目模仿IOS它的设计,由于A ...
- state-of-the-art implementations related to visual recognition and search
http://rogerioferis.com/VisualRecognitionAndSearch2014/Resources.html Source Code Non-exhaustive lis ...
- RH253读书笔记(10)-Appendix A Installing Software
Appendix A Installing Software Below are a few methods to locate and install required packages. You ...
- 余弦信号DFT频谱分析(继续)
以前谈到序列的实际长度可以通过零填充方法加入,使得最终增加N添加表观分辨率. 但它并没有解决泄漏频率的问题. 根本原因在于泄漏窗口选择的频率. 由于矩形窗突然被切断,频谱旁瓣相对幅度过大,造成泄漏分量 ...