Last active
August 29, 2015 14:16
-
-
Save hitjim/b950caa7791d07def825 to your computer and use it in GitHub Desktop.
array concat apply
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
// Want to control subsort of each container, but push the results sequentially | |
// by container on resultsArray | |
// Otherwise if you just .push like normal on resultsArray, you'll clobber | |
// at the top of whatever looping construct you use. | |
var i, j, resultsArray = []; | |
var subArray1First = true; | |
var sourceArrays = [ | |
container1 = { | |
subArray1: [a, b, c], | |
subArray2: [d, e, f] | |
}, | |
container2 = { | |
subArray1: [g, h, i], | |
subArray2: [ [k, l, m], [n, o, p] ] | |
} | |
]; | |
for (i = 0; i < sourceArrays.length; i++) { | |
container = sourceArrays[i]; | |
// It gets a little lengthly when using concat instead of push, but this way you can do it all in one call. | |
if (subArray1first) { | |
resultsArray = resultsArray.concat.apply(resultsArray, container.subArray1, container.subArray2); | |
} else { | |
resultsArray = resultsArray.concat.apply(resultsArray, container.subArray2, container.subArray1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment