Flatten/Nest Javascript objects