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] Create an Array concatAll method的更多相关文章

  1. JavaScript interview Question - Create a Array with two papameters without using loop!

    JavaScript interview Question - Create a Array with two papameters without using loop! JavaScript - ...

  2. [Javascript + rxjs] Using the map method with Observable

    Like an array, Observable has a map method that allows us to transform a sequence into a new Observa ...

  3. [Python Cookbook] Numpy: Multiple Ways to Create an Array

    Convert from list Apply np.array() method to convert a list to a numpy array: import numpy as np myl ...

  4. javascript类型系统之Array

    原文:javascript类型系统之Array 目录 [1]数组创建 [2]数组操作 [3]继承的方法 [4]实例方法 数组转换 数组检测 栈和队列 排序方法 操作方法 位置方法 前面的话 数组是一组 ...

  5. ExtJS学习-----------Ext.Array,ExtJS对javascript中的Array的扩展

    关于ExtJS对javascript中的Array的扩展.能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 因为 ...

  6. 从头开始学JavaScript (十二)——Array类型

    原文:从头开始学JavaScript (十二)--Array类型 一.数组的创建 注:ECMAscript数组的每一项都可以保存任何类型的数据 1.1Array构造函数 var colors = ne ...

  7. 【转】javascript Object使用Array的方法

    原文: http://www.cnblogs.com/idche/archive/2012/03/17/2403894.html Array.prototype.push push向数组尾部添加一项并 ...

  8. JavaScript中数组Array方法详解

    ECMAScript 3在Array.prototype中定义了一些很有用的操作数组的函数,这意味着这些函数作为任何数组的方法都是可用的. 1.Array.join()方法 Array.join()方 ...

  9. JavaScript高级编程——Array数组迭代(every()、filter()、foreach()、map()、some(),归并(reduce() 和reduceRight() ))

    JavaScript高级编程——Array数组迭代(every().filter().foreach().map().some(),归并(reduce() 和reduceRight() )) < ...

随机推荐

  1. [状压dp]HDOJ4539 郑厂长系列故事——排兵布阵

    中文题,题意不再赘述 对于“?”这一格,它所能攻击到的(曼哈顿距离为2的) 前方的 即“√”的四个位置 那么与此格有关的即它前方两行(即状压这两行) 首先预处理每行能满足的: i 和(i<< ...

  2. File List()列出文件目录

    import java.io.File; public class FileTest { public static void main(String[] args) { File myFile = ...

  3. velocity-1.7学习笔记

    Velocity是由Apache软件组织提供的一项开放源码项目,它是一个基于Java的模板引擎.通过Velocity模板语言(Velocity Template Language,VTL)定义模板(T ...

  4. Good vs Evil

    Good vs Evil Description Middle Earth is about to go to war. The forces of good will have many battl ...

  5. 应付系统选项 Payables Options

    (N) AP > Setup > Options > Payables Options应付系统选项设置整个应付系统使用的控制项和默认值.我们可以在此窗口中设置默认值,从而简化供应商输 ...

  6. hdu2852KiKi's K-Number(区间K值)

    http://acm.hdu.edu.cn/showproblem.php?pid=2852 区间K值写错了... #include <iostream> #include<cstd ...

  7. MapReduce架构设计

    MapReduce采用Master/Slave的架构,其架构图如下: 它主要有以下4个部分组成: 1)Client 2)JobTracker JobTracke负责资源监控和作业调度.JobTrack ...

  8. POJ 3083 Children of the Candy Corn 解题报告

    最短用BFS即可.关于左手走和右手走也很容易理解,走的顺序是左上右下. 值得注意的是,从起点到终点的右手走法和从终点到起点的左手走法步数是一样. 所以写一个左手走法就好了.贴代码,0MS #inclu ...

  9. Chrome的隐身模式

    先来说说隐身模式的启用方法吧 1.键盘快捷:Ctrl + Shift + N. 2.在Windows7下的任务栏处,右击“Chrome”图标,会出一个下拉菜单,点击“新建隐身窗口”. 3.你还可以在一 ...

  10. c#中字符串截取使用的方法

    AndyZhang welcome to java world c#中字符串截取使用的方法 String substring(int beginIndex) String substring(int ...