Skip to content

Instantly share code, notes, and snippets.

@dmarchuk
Last active July 20, 2018 13:44
Show Gist options
  • Save dmarchuk/deb12ae027e97a43ad5f992692c04426 to your computer and use it in GitHub Desktop.
Save dmarchuk/deb12ae027e97a43ad5f992692c04426 to your computer and use it in GitHub Desktop.
Split arrays into an object of arrays (by string before "_")
function splitArrays(arrayData) {
var data = arrayData;
var arrays = {};
if(data) {
data.forEach(function (str) {
// Get id piece
str_api = str.substring(0, str.indexOf('_'));
// check if existing property for this id, if not initialize new array
if (!arrays[str_api]) {
arrays[str_api] = [];
}
// get value piece
str_id = str.substring(str.indexOf() + 1);
// add to that id's array
arrays[str_api].push(str_id);
});
}
return arrays;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment