[Javascript] The Array filter method
One very common operation in programming is to iterate through an Array's contents, apply a test function to each item, and create a new array containing only those items the passed the test. For example, let's say you wanted to loop through an array of stocks and select only those with the price larger than a certain value. In this lesson we will demonstrate how to use the Array's filter method to easily perform this operation with less code than a loop would require.
function getStocksOver(stocks, minPrice) {
return stocks.filter(function(stock) {
return stock.price >= minPrice;
})
} var expensiveStocks = getStocksOver([
{ symbol: "XFX", price: 240.22, volume: 23432 },
{ symbol: "TNZ", price: 332.19, volume: 234 },
{ symbol: "JXJ", price: 120.22, volume: 5323 },
],
150.00); console.log(JSON.stringify(expensiveStocks));
[Javascript] The Array filter method的更多相关文章
- [Javascript] The Array forEach method
Most JavaScript developers are familiar with the for loop. One of the most common uses of the for lo ...
- [Javascript] The Array map method
One very common operation in programming is to iterate through an Array's contents, apply a function ...
- JavaScript笔记Array.filter(Boolean)
ECMAScirpt5 中 Array 类中的 filter 方法使用目的是移除所有的 ”false“ 类型元素 (false, null, undefined, 0, NaN or an empt ...
- JavaScript的Array.prototype.filter()详解
摘抄与:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter 概述 ...
- JavaScript Array filter() 方法
JavaScript Array filter() 方法 var ages = [32, 33, 16, 40]; function checkAdult(age) { return age > ...
- JavaScript 中Array数组的几个内置函数
本文章内容均参考<JavaScript高级程序设计第三版> 今天在看JavaScript书籍的时候,看到之前没有了解过的JavaScript中Array的几个内置函数对象,为了之后再开发工 ...
- Filter method example
The Scala List class filter method implicitly loops over the List/Seq you supply, tests each element ...
- JavaScript中Array的正确使用方式
在 JavaScript 中正确使用地使用 Array 的方法如下: 用 Array.includes 代替 Array.indexOf “如果你要在数组中查找元素,请使用 Array.indexOf ...
- js array filter pop push shift unshift方法
JavaScript Array filter() 方法 JavaScript Array 对象 实例 返回数组 ages 中所有元素都大于 18 的元素: var ages = [32, 33, ...
随机推荐
- RxJava开发精要4 – Observables过滤
原文出自<RxJava Essentials> 原文作者 : Ivan Morgillo 译文出自 : 开发技术前线 www.devtf.cn 转载声明: 本译文已授权开发者头条享有独家转 ...
- Android 设置按钮为透明
设置一个按钮为透明, (1)修改配置文件 <Button android:id="@+id/btnAppMore" android:layout_width="wr ...
- maven 的 oracle的Missing artifact com.oracle:******:jar:11.2.0.2.0
解决方法: 下载ojdbc6对应版本号的包.把下载的包放到: 然后dos命令: -Dpackaging=jar -Dfile=ojdbc6.jar 主:必须确保已经配置了你maven,如何配置mave ...
- 创建WCF的客户端
How to: Create a Windows Communication Foundation Client To create a Windows Communication Foundatio ...
- scaleform 注意事项
在使用 自带的UI .fla 里面的组建时 需要把自己建立的fla进行如下设置. 文件-发布设置-flash-脚本actionscript3.0设置——舞台:自动声明舞台实例
- C#面向对象编程实例-猜拳游戏
1.需求 现在要制作一个游戏,玩家与计算机进行猜拳游戏,玩家出拳,计算机出拳,计算机自动判断输赢. 2.需求分析 根据需求,来分析一下对象,可分析出:玩家对象(Player).计算机对象(Comput ...
- NEsper事件处理 z
http://esper.codehaus.org/nesper/documentation/documentation.html 环境配置 NEsper库下载:下载网址 Vs2010环境集成 在项目 ...
- HDU-5391 Zball in Tina Town
(n-1)!/n 就是如果n为素数,就等于n-1else为0. 求素数表: Zball in Tina Town Time Limit: 3000/1500 MS (Java/Others) Memo ...
- GTK+系统中的对话框(GTK+dialogs)
GTK+系统中的对话框(GTK+dialogs) GTK+系统中的对话框(GTK+ dialogs) 在接下来的章节中我们将着重介绍GTK+系统中的对话框. 对话框窗口是众多GUI应用程序中不可或缺的 ...
- aggregate 和 treeAggregate 的对比
1.定义 [aggregate] /** * Aggregate the elements of each partition, and then the results for all the pa ...