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. [Gauss]POJ2947 Widget Factory

    题意: 有n种小工具要加工,每种工具的加工时间为3到9天,给了m条加工记录.  每条记录 X $s_1$ $s_2$ 分别代表 这个工人在$s_1$到$s_2$(前闭后闭)的时间里加工了X件小工具   ...

  2. div+css实现导航示意箭头

    1.Div的宽高为100 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <h ...

  3. Android开发之AlertDialog

    http://www.cnblogs.com/Gaojiecai/archive/2011/12/10/2283156.html http://www.2cto.com/kf/201205/13187 ...

  4. MySQL information_schema表查询导致内存暴涨

    case:下面的一条sql语句,导致mysql实例内存暴涨: select * from tables where table_name not in(select table_name from p ...

  5. WebChart网页局域网聊天系列(一):ActiveX插件编写

    第一步:创建ActiveX控件类库,在解决方案中右击添加Window窗体控件库 在该类库属性中,设置 使程序集COM可见,同时设置为COM互操作注册 另外在自动生成的文件中AssemblyInfo.c ...

  6. Linux Kernel ‘/net/socket.c’本地信息泄露漏洞

    漏洞名称: Linux Kernel ‘/net/socket.c’本地信息泄露漏洞 CNNVD编号: CNNVD-201312-037 发布时间: 2013-12-04 更新时间: 2013-12- ...

  7. mysql备份脚本

    [root@AY130828161048465847Z ~]# vi mysqlbak.sh #!/bin/bash USERNAME=rootPASSWORD=mysqlDBNAME=test DA ...

  8. HtmlParser应用,使用Filter从爬取到的网页中获取需要的内容

    htmlparser是一个纯的java写的html解析的库,它不依赖于其它的java库文件,主要用于改造或提取html.它能超高速解析html,而且不会出错.现在htmlparser最新版本为2.0. ...

  9. bzoj2763: [JLOI2011]飞行路线 分层图+dij+heap

    分析:d[i][j]代表从起点到点j,用了i次免费机会,那就可以最短路求解 #include <stdio.h> #include <iostream> #include &l ...

  10. 多线程归并排序的实现 java

    多线程是非常适合归并排序的,因为归并排序是分治法,所以分割后可以独立运行,最后将结果归并起来就行了.如何写一个多线程程序呢?今天无聊,总结一下啊. 首先写个普通的归并排序,以后的多线程就调用这个排序. ...