[Javascript] Create an Array concatAll method
JavaScript Array an create method
2023-09-14 09:00:55 时间
In addition to flat Arrays, programmers must often deal with nested Arrays. For example let's say we have an Array of stock exchanges, each of which is represented by an array of all the stocks listed on that exchange. If we were looking for a stock that matched a certain criteria, we would first need to loop through all of the exchanges, and then all of the stocks within.
In these situations, most developers would nest two loops. However in this lesson we will write a new Array function "concatAll" which will automatically flatten nested arrays buy one dimension. This will remove the need to ever use a nested loop to flatten a nested array.
var exchanges = [ [ { symbol: "XFX", price: 240.22, volume: 23432 }, { symbol: "TNZ", price: 332.19, volume: 234 } ], [ { symbol: "JXJ", price: 120.22, volume: 5323 }, { symbol: "NYN", price: 88.47, volume: 98275 } ] ]; Array.prototype.concatAll = function() { var results = []; this.forEach(function(subArray) { subArray.forEach(function(item) { results.push(item); }); }); return results; }; var stocks = exchanges.concatAll(); stocks.forEach(function(stock) { console.log(JSON.stringify(stock)); });
Also see lodash flatten:
_.flatten(array, [isDeep])
https://lodash.com/docs#flatten
相关文章
- JavaScript Array数组分页
- 【说站】javascript中Array.slice()如何使用
- 【说站】javascript中Array.join()方法如何使用
- javascript 高级编程 之 Array 用法总结
- JavaScript ArrayJavaScript博客 1 年前 桃李Taoli
- winform与php交互,WinForm与Javascript交互「建议收藏」
- JavaScript动画基本原理
- Magniber勒索软件通过JavaScript文件感染Windows用户
- WordPress 教程:如何正确引用 JavaScript 和 CSS 文件
- JavaScript京东左侧悬浮导航制作详解编程语言
- JavaScript学习总结(二十)——Javascript非构造函数的继承详解编程语言
- Javascript中的Array(数组) 、{}(映射) 与JSON解析详解编程语言
- array用Redis轻松操控Byte Array(redisbyte)
- 使用JavaScript在Oracle中执行函数(js执行oracle函数)
- javascript编程起步(第七课)
- Javascript常用运算符(Operators)-javascript基础教程
- javascript删除Table中的一行的脚本代码
- IE和Firefox下javascript的兼容写法小结
- JavaScript对象链式操作测试代码
- JavaScript设计模式富有表现力的Javascript(一)
- javascript学习笔记(五)Array数组类型介绍
- JavaScript高级程序设计(第3版)学习笔记7js函数(上)
- javascript:文字不间断向左移动的实例代码
- javascript版的in_array函数(判断数组中是否存在特定值)
- JavaScript中伪协议javascript:使用探讨
- Javascript基础教程之JavaScript语法
- JavaScript实现的in_array函数