zoukankan      html  css  js  c++  java
  • [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));
  • 相关阅读:
    python03-if
    python03
    基础知识梳理
    开篇话
    托管代码---> CLR --> 自宿主
    反射定义及基础案例
    c# 中委托的发展
    委托代码案例
    委托(实例)
    字节(Byte) 与 位(bit)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/4356391.html
Copyright © 2011-2022 走看看