Javascript Flatten Array Into Object Oriented Database Structure