-2

I have data in localStorage set with user details like this

{"name":"aa","email":"[email protected]","password":"aaaa","confirm_password":"aaaa"}

and another one like

{"otp":"1235"}

Now, I want to retrieve this as an single object like this

{"name":"aa","email":"[email protected]","password":"aaaa","confirm_password":"aaaa","otp":"1235"}

Please help. Thanks.

4
  • How are you reading them out of a local storage and assigning them to vars? Commented Nov 18, 2019 at 10:47
  • Use object destructuring Commented Nov 18, 2019 at 10:49
  • 1
    Object.assign(obj1, obj2); or Object.assign({}, obj1, obj2); (I consider you casted the localstorage string to a json object) Commented Nov 18, 2019 at 10:49
  • With their keys like localStorage.getItem('userData'); & for another localStorage.getItem('otp'); Commented Nov 18, 2019 at 10:49

2 Answers 2

0

Use object destructuring:

const obj1 = JSON.parse('{"name":"aa","email":"[email protected]","password":"aaaa","confirm_password":"aaaa"}');
const obj2 = JSON.parse('{"otp":"1235"}');
const obj3 = { ...obj1, ...obj2 };

console.log(obj3);

Sign up to request clarification or add additional context in comments.

Comments

0
jsonArray1 = {"name":"aa","email":"[email protected]","password":"aaaa","confirm_password":"aaaa"};
jsonArray2 = {"otp":"1235"};
 Object.assign(jsonArray1, jsonArray2);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.