Last active
October 31, 2016 18:35
-
-
Save kutyel/550194495556414aa61e59feabd4ad35 to your computer and use it in GitHub Desktop.
Order an array given another array with its indices
This file contains hidden or 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
const A = ["d", "a", "c", "b"]; | |
const I = [3, 0, 2, 1]; | |
A.reduce((x, y, i, arr) => x.concat(arr[I.indexOf(i)]), []); | |
// > ["a", "b", "c", "d"] | |
// Functional Programming Power! | |
const sortExternalFromArray = z => (x, y, i, arr) => x.concat(arr[z.indexOf(i)]); | |
// A more elegant/reusable approach | |
A.reduce(sortExternalFromArray(I), []); | |
// > ["a", "b", "c", "d"] // Still working! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment