Javascript Flatten Array Into Object Oriented Paradigm