zoukankan      html  css  js  c++  java
  • [Javascript] Chaining the Array map and filter methods

    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);
    })
  • 相关阅读:
    easyui
    mvc
    Servlet简单计算器 2.0
    简易Servlet计算器1.0
    javaBean 练习—封装学生信息
    application和javaBean练习
    远程存储程序
    通讯录
    黑名单管理代码总结
    DAO
  • 原文地址:https://www.cnblogs.com/Answer1215/p/4356399.html
Copyright © 2011-2022 走看看