Refactor add object logic in js