I'm using React, Redux & immutable. I tried to merge two Json objects, data and expected result is given below,
obj1 =
{id: 1, first_name: "abc", surname: "def", children: ["kid1", "kid2"]}
obj2 =
{first_name: "abc", surname: "def", email: "[email protected]", mobile: ""}
mergedObj = {id: 1, first_name: "abc", surname: "def", email: "[email protected]", Mobile: "", children: ["kid1", "kid2"]}
If data is not available on obj1, it should be taken it from obj2.
I tried using immutable merge as outlined below,
import Immutable, {merge} from "immutable";
const mergedObj = merge(Immutable.fromJS(obj2), Immutable.fromJS(obj1));
console.log(mergedObj)
Result is same as obj2, essentially, merge is not happening,
{first_name: "abc", surname: "def", email: "[email protected]", mobile: ""}
You can do merge using javascript spread operator which is introduced in ES6 onwards.
var obj1 = {id: 1, first_name: "abc", surname: "def", children: ["kid1", "kid2"]}
var obj2 = {first_name: "abc", surname: "def", email: "[email protected]", mobile: ""}
var mergedObj = {...obj1,...obj2};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With