您的位置:首页 > Web前端 > JavaScript

[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);
})
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: