Skip to content

Instantly share code, notes, and snippets.

@JoaoCnh
Created February 14, 2018 14:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JoaoCnh/cc2267a72e6e5227caea4ede41459a53 to your computer and use it in GitHub Desktop.
Save JoaoCnh/cc2267a72e6e5227caea4ede41459a53 to your computer and use it in GitHub Desktop.
Array of Arrays flatten with reduce
const mult = [songs, [{id: 112, name: "Chop Suey", artist: "System of a Down" }]];
var flatMult = mult.reduce(function (acc, currValue) {
return acc.concat(currValue);
}, []);
// ES6
const flatMult = mult.reduce((acc, currValue) => {
return acc.concat(currValue);
}, []);
console.log(flatMult);
// [
// { id: 1, name: "Curl of the Burl", artist: "Mastodon" },
// { id: 2, name: "Oblivion", artist: "Mastodon" },
// { id: 3, name: "Flying Whales", artist: "Gojira" },
// { id: 4, name: "L'Enfant Sauvage", artist: "Gojira" },
// { id: 112, name: "Chop Suey", artist: "System of a Down" },
// ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment