[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, ...
随机推荐
- strspn和strcspn妙用
http://blog.csdn.net/aidenliu/article/details/5460201
- 自定义的IntentFileter 无法找到activity
<intent-filter > <action android:name="com.leo.enjoytime.VIEW"/></intent-fi ...
- 【HDOJ】1196 Lowest Bit
水题,原理是计算机组成原理中的负数的补码的求码.利用按位与可解. #include <iostream> using namespace std; int main() { int n; ...
- 应付系统选项 Payables Options
(N) AP > Setup > Options > Payables Options应付系统选项设置整个应付系统使用的控制项和默认值.我们可以在此窗口中设置默认值,从而简化供应商输 ...
- c/c++ void 指针
原文 : http://blog.csdn.net/yyyuhan/article/details/3153290 1.概述 许多初学者对C/C++语言中的void及void指针类型不甚理解,因此在使 ...
- 使用PowerShell脚本部署定时器到MOSS2010
转:http://www.77site.com/tech/1087042010072906074113_2012050808152911.html 第一章 前言 在此次练习中,您将了解到如何使用Pow ...
- geoserver 安装 sql server 插件
Did you download and install the sql server plugin as well? http://downloads.sourceforge.net/proje ...
- 解决Dagger2和butterknife冲突
dagger2 和 RxJava butterknife 以及 Retrofit使用起来非常酸爽 代码非常干净清晰 动手尝试 配置编译 DaggerAppComponent的时候 出现问题 配置dag ...
- ZOJ 3795 Grouping
大致题意是给n个人和m组关系,每组关系都是两个人s和t,表示s年龄不小于t的年龄,然后让你把这n个人分组,使得任何一个组里面的任意两人都不能直接或间接的得出这两个人的年龄大小关系. 思路:根据给出的关 ...
- ZOJ 3791 An Easy Game
思路:dp+记忆化搜索,设dp[n][m]表示s1与s2不同字符个数为n,还需要变m步的方法数,那么: dp[n][m] = (c[n][i]*c[N-n][K-i]) * dp[n-i+(K-i ...