[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, ...
随机推荐
- dojo 图表制作教程
http://www.sitepen.com/labs/code/charting/tutorial/tutorial1.html http://www.sitepen.com/labs/code/c ...
- mysql联合索引
命名规则:表名_字段名1.需要加索引的字段,要在where条件中2.数据量少的字段不需要加索引3.如果where条件中是OR关系,加索引不起作用4.符合最左原则 https://segmentfaul ...
- 【HDOJ】1709 The Balance
母函数,指数可以为1也可以为-1,扩大指数加消减发现TLE,于是采用绝对值就过了. #include <stdio.h> #include <string.h> #define ...
- Hello Indigo
Windows Communication Foundation (WCF),formerly code-named “Indigo,” is Microsoft’s platform for Ser ...
- LightOJ 1214 Large Division 水题
java有大数模板 import java.util.Scanner; import java.math.*; public class Main { public static void main( ...
- POJ 3159 Candies 差分约束dij
分析:设每个人的糖果数量是a[i] 最终就是求a[n]-a[1]的最大值 然后给出m个关系 u,v,c 表示a[u]+c>=a[v] 就是a[v]-a[u]<=c 所以对于这种情况,按照u ...
- Fiddler On Linux
参考链接: http://www.development-cycle.com/2013/08/debugging-web-applications-with-fiddler-on-linux/ htt ...
- SSAS使用MDX生成脱机的多维数据集CUB文件
在运用多维数据进行分析的时候,通常很有可能我们需要把这些多维数据脱机进行处理或演示,这其中就要用到cub文件 http://www.cnblogs.com/yunhuasheng/archive/20 ...
- oracle创建库和表
SQL> create user midamtemp identified by ty1234 default tablespace midamtemp; User created. --分配用 ...
- 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程03:碰撞检测》
3.碰撞检测 碰撞检测的概述: 碰撞在物理学中表现为两粒子或物体间极端的相互作用.而在游戏世界中,游戏对象在游戏世界自身并不受物理左右,为了模拟真实世界的效果,需要开发者为其添加属性,以模拟真实事件的 ...