[Javascript] The Array filter method
JavaScript The Array filter method
2023-09-14 09:00:55 时间
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的DOM总结
- JavaScript中统计字符的个数
- JavaScript多个音频audio标签或者多个视频video,点击其中一个播放时,其他的停止播放
- 【JavaScript】快速入门
- [Jest] Restore the Original Implementation of a Mocked JavaScript Function with jest.spyOn
- [Javascript] Finding Parent Elements
- [Javascript] Automate the process of flattening deeply nested arrays using ES2019's flat method
- [Javascript] Logging Pretty-Printing Tabular Data to the Console
- [Javascript] lodash: memoize() to improve the profermence
- [Javascript] The Array filter method
- [Javascript] Gradient Fills on the HTML5 Canvas
- javascript: 复制对象时的深拷贝及浅拷贝(chrome 105.0.5195.125)
- [Javascript] Broadcaster + Operator + Listener pattern -- 26. Share the Same Broadcaster Values Across Multiple Listeners
- [Javascript] Yield an Array, String, or Any Iterable from a Generator (yield*)
- [Javascript] Understanding the .constructor property on JavaScript Objects
- [Javascript Crocks] Understand the Maybe Data Type
- [Javascript] Identify the most important words in a document using tf-idf in Natural
- [Javascript] Validate Data with the Every() Method
- [Javascript ] Array methods in depth - sort
- [Javascript] Introducing Reduce: Common Patterns
- [Javascript] The Array map method
- [Javascript] Gradient Fills on the HTML5 Canvas
- JavaScript自定义排序条件
- 从零开始学_JavaScript_系列(22)——dojo(9)(表单、JsonRest的post方法,widget的使用思路)
- 【华为OD机试 2023】获取最大软件版本号(C++ Java JavaScript Python)
- JavaScript学习总结(二)——延迟对象、跨域、模板引擎、弹出层、AJAX示例
- 每天一个JavaScript实例-推断图片是否载入完毕