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的更多相关文章

  1. [Javascript] The Array forEach method

    Most JavaScript developers are familiar with the for loop. One of the most common uses of the for lo ...

  2. [Javascript] The Array map method

    One very common operation in programming is to iterate through an Array's contents, apply a function ...

  3. JavaScript笔记Array.filter(Boolean)

    ECMAScirpt5 中 Array 类中的 filter 方法使用目的是移除所有的 ”false“ 类型元素  (false, null, undefined, 0, NaN or an empt ...

  4. JavaScript的Array.prototype.filter()详解

    摘抄与:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter 概述 ...

  5. JavaScript Array filter() 方法

    JavaScript Array filter() 方法 var ages = [32, 33, 16, 40]; function checkAdult(age) { return age > ...

  6. JavaScript 中Array数组的几个内置函数

    本文章内容均参考<JavaScript高级程序设计第三版> 今天在看JavaScript书籍的时候,看到之前没有了解过的JavaScript中Array的几个内置函数对象,为了之后再开发工 ...

  7. Filter method example

    The Scala List class filter method implicitly loops over the List/Seq you supply, tests each element ...

  8. JavaScript中Array的正确使用方式

    在 JavaScript 中正确使用地使用 Array 的方法如下: 用 Array.includes 代替 Array.indexOf “如果你要在数组中查找元素,请使用 Array.indexOf ...

  9. js array filter pop push shift unshift方法

    JavaScript Array filter() 方法  JavaScript Array 对象 实例 返回数组 ages 中所有元素都大于 18 的元素: var ages = [32, 33,  ...

随机推荐

  1. Cassandra命令行CLI的基本使用

    启动cassandra-cli服务之后,可以进行CQL的使用. 1. 创建keyspace 可以理解成关系数据库的database [default@testkeyspace] create keys ...

  2. 交叉编译 小米路由器mini 的 python(MIPS)

    看了很多文章,要么说的是用opkg安装python,要么说的是小米路由器的交叉编译,就是没有mini的.学习了这篇文章(http://me.deepgully.com/post/56389167868 ...

  3. hadoop2.2编程:MRUnit

    examples: Overview This document explains how to write unit tests for your map reduce code, and test ...

  4. 深刻理解C#的传值调用和传引用调用

    传值调用和传引用调用是几乎所有主流语言都会涉及到的问题,下面我谈谈我对C#中传值调用和传引用调用的理解. 1. 一般对C#中传值调用和传引用调用的理解 如果传递的参数是基元类型(int,float等) ...

  5. JDK/bin目录下的不同exe文件的用途(转)

    新安装完JDk 大家是否发现安装目录的bin文件夹有很多exe文件 下面就为大家讲解不同exe文件的用途 javac:Java编译器,将Java源代码换成字节代 java:Java解释器,直接从类文件 ...

  6. breakpoints、lldb 和 chisel 的使用

    http://www.cocoachina.com/ios/20150803/12805.html Breakpoints BreakPoint分类 breakpoint也是有分类的,我这里的文章内大 ...

  7. linux文件属性详解

    Linux 文件或目录的属性主要包括:文件或目录的节点.种类.权限模式.链接数量.所归属的用户和用户组.最近访问或修改的时间等内容.具体情况如下: 命令: ls -lih 输出: [root@loca ...

  8. spring--AOP1--6

    AOP 之 6.1 AOP基础 6.1.1  AOP是什么 考虑这样一个问题:需要对系统中的某些业务做日志记录,比如支付系统中的支付业务需要记录支付相关日志,对于支付系统可能相当复杂,比如可能有自己的 ...

  9. html input设置为只读属性

    有两种方式可以实现input的只读效果:disabled 和 readonly. 自然两种出来的效果都是只能读取不能编辑,可是两者有很大不同. Disabled说明该input无效,及其value不会 ...

  10. 多线程模式之MasterWorker模式

    多线程模式之MasterWorker模式 Master-Worker模式的核心思想是,系统由两类进程协作工作:Master进程和Worker进程.Master进程负责接收和分配任务,Worker进程负 ...