Skip to content

Instantly share code, notes, and snippets.

View Pau1fitz's full-sized avatar

Paul Fitzgerald Pau1fitz

View GitHub Profile
@Pau1fitz
Pau1fitz / flatten.js
Created September 22, 2019 09:17
Flatten multi-dimensional array
const flatten = (arr) => arr.reduce(function (flat, toFlatten) {
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
}, []);
const multiDimensionalArray = [[1,2,[3]],4];
const flattenedArray = flatten(multiDimensionalArray);
it('flattens an array of arbitrarily nested arrays of integers into a flat array of integers', () => {
assert.equal(flatten([[1,2,[3]],4]), [1, 2, 3, 4])
})
{"version":1,"resource":"file:///Users/paulfitzgerald/Desktop/app-eng/code/data/ui/src/swift/MatchesPage.tsx","entries":[{"id":"aBx8.tsx","timestamp":1657879035527},{"id":"te8x.tsx","timestamp":1657879052777}]}