Skip to content

Instantly share code, notes, and snippets.

@Biacode
Created October 11, 2017 06:42
Show Gist options
  • Save Biacode/427a5177afec901900a06fa6e2b7d8b0 to your computer and use it in GitHub Desktop.
Save Biacode/427a5177afec901900a06fa6e2b7d8b0 to your computer and use it in GitHub Desktop.
JavaScript FlatMap
Array.prototype.flatMap = function (lambda) {
return Array.prototype.concat.apply([], this.map(lambda));
};
var foo = [
[
[{"uuid": 1, "name": "name_1"}],
[{"uuid": 2, "name": "name_2"}],
[{"uuid": 3, "name": "name_3"}]
],
[
[{"uuid": 4, "name": "name_4"}],
[{"uuid": 5, "name": "name_5"}]
],
[
[{"uuid": 6, "name": "name_6"}],
[{"uuid": 7, "name": "name_7"}],
[{"uuid": 8, "name": "name_8"}],
[{"uuid": 9, "name": "name_9"}]
]
];
var bar = foo.flatMap(function (first) {
return first.flatMap(function (second) {
return second;
});
});
var goo = {};
bar.forEach(function (it) {
goo[it.uuid] = it.name;
});
console.log(goo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment