Skip to content

Instantly share code, notes, and snippets.

@hitjim
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitjim/b950caa7791d07def825 to your computer and use it in GitHub Desktop.
Save hitjim/b950caa7791d07def825 to your computer and use it in GitHub Desktop.
array concat apply
// 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