Last active
March 10, 2016 11:29
-
-
Save ItsAsbreuk/66eca46fa4399ca3c198 to your computer and use it in GitHub Desktop.
Flattening nested array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"presets": ["es2015"] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// API-documentation generated by yuidoc: http://yui.github.io/yuidoc/ | |
/** | |
* | |
* Flattens a nested array | |
* | |
* <i>Copyright (c) 2016 Marco Asbreuk</i> | |
* New BSD License - http://choosealicense.com/licenses/bsd-3-clause/ | |
* | |
* @module array | |
* @class Array | |
* | |
*/ | |
"use strict"; | |
/** | |
* Flattens an Array (nested Array-items become items of the array) | |
* | |
* @method itsa_flatten | |
* @param source {Array} the source-array that needs to be flattened | |
* @return Array the flattened array | |
*/ | |
const flattenArray = source => { | |
let flattened = Array.prototype.concat.apply([], source), | |
prevLen = flattened.length; | |
// concat will only flatten 1 level deep, so in case of nested arrays, | |
// we might need more flattens. We check this by checking the lenght of the flattened array. | |
// If it doesn't increase anymore, then we are finished | |
do { | |
flattened = Array.prototype.concat.apply([], flattened); | |
} while (prevLen !== (prevLen = flattened.length)); | |
return flattened; | |
}; | |
export default flattenArray; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "flatten-array", | |
"version": "0.0.1", | |
"description": "Flattens a nested array", | |
"main": "flatten-array.js", | |
"scripts": { | |
"test": "mocha test.js --compilers js:babel-core/register" | |
}, | |
"author": "Marco Asbreuk", | |
"license": "BSD-3-Clause", | |
"dependencies": {}, | |
"devDependencies": { | |
"babel-core": "^6.7.0", | |
"babel-preset-es2015": "^6.6.0", | |
"chai": "^3.5.0", | |
"mocha": "^2.4.5" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global describe, it */ | |
"use strict"; | |
import {expect} from "chai"; | |
import flatten from "./flatten-array"; | |
describe("Testing flattenArray", () => { | |
const flattened = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
it("Should leave 1-dim array the same", () => { | |
var source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
expect(flatten(source)).to.be.eql(flattened); | |
}); | |
it("Should flatten 2-dim array", () => { | |
var source = [1, [2, 3], 4, 5, [6, 7], 8, 9, 10]; | |
expect(flatten(source)).to.be.eql(flattened); | |
}); | |
it("Should flatten 3-dim array", () => { | |
var source = [1, [2, [3, 4]], [5, [6, 7], 8], 9, 10]; | |
expect(flatten(source)).to.be.eql(flattened); | |
}); | |
it("Should flatten 4-dim array", () => { | |
var source = [1, [2, [3, [4, 5], 6, 7], 8], 9, 10]; | |
expect(flatten(source)).to.be.eql(flattened); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment