[Javascript] Chaining the Array map and filter methods
2015-03-21 22:16
363 查看
Both map and filter do not modify the array. Instead they return a new array of the results. Because both map and filter return Arrays, we can chain these functions together to build complex array transformations with very little code. Finally we can consume the newly created array using forEach. In this lesson, we will learn how to build nontrivial programs without using any loops at all.
var stocks = [ { symbol: "XFX", price: 240.22, volume: 23432 }, { symbol: "TNZ", price: 332.19, volume: 234 }, { symbol: "JXJ", price: 120.22, volume: 5323 }, ]; var filteredStockSymbols = stocks. filter(function(stock) { return stock.price >= 150.00; }). map(function(stock) { return stock.symbol; }) filteredStockSymbols.forEach(function(symbol) { console.log(symbol); })
相关文章推荐
- Javascript Object Method Properties console.log View all methods and properties of the object
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- [Javascript] Array methods in depth - filter
- [Javascript] Ex: concatAll, map and filter
- JavaScript "filter" and "map"
- [Javascript] The Array map method
- Advanced JavaScript Array Methods: fill and reverse
- 计算数组中最大值、最小值和平均值的方法(The methods of calculating the maximum, minimum and average of a array )
- [Javascript] The Array filter method
- Use Flex and JavaScript to get the full path of a local File
- how to accepte the array of the checkbox in asp and in php
- Motherboard Chipsets and the Memory Map
- Custom Alert Boxes using JavaScript and the DOM
- Prototype and Scriptaculous: Taking the Pain out of JavaScript
- JavaScript and Ajax for the Web, Sixth Edition
- 学习 JavaScript and Ajax for the Web (Sixth Edition)
- The Javascript alert, confirm, and prompt boxes
- Multimedia Content and the Semantic Web: Standards, Methods and Tools
- 【原】用使用JavaScript展开/折叠TreeView中所有节点(Expand and Collapse All Nodes of asp.net Treeview on the client with javascript)
- ocx function and the order of BEGIN_DISPATCH_MAP