Flatten/unflatten nested Javascript objects