Last active
July 20, 2018 13:44
-
-
Save dmarchuk/deb12ae027e97a43ad5f992692c04426 to your computer and use it in GitHub Desktop.
Split arrays into an object of arrays (by string before "_")
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
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