0

I've an object as below:

default = {
    name: "",
    details: {
        age: "",
        place: ""
        country: ""
    },
    contact: {
        email: null,
        phone: null
    }
}

Now, I've another object as:

data = {
    name: "Abc",
    details: {
        age: "27,
    },
    contact: {
        email: "abc@xyz.com"
    }
}

Want to achieve:

result = {
    name: "Abc",
    details: {
        age: 27,
        place: ""
        country: ""
    },
    contact: {
        email: "abc@xyz.com",
        phone: null
    }
}

Now, I want to get the values from data and put it in default. Currently I'm checking if the key exists, then assign value, else keep default. But it's a long process. Is there a JS helper function or something that I can use for this?

Object.assign(default, data) is only copying the property values and not going deep.

user1012181
  • 8,648
  • 10
  • 64
  • 106
  • It's not the same one as Mike C suggested. It's a completely different situation. – user1012181 Oct 18 '17 at 19:59
  • I'm looking for deep cloning. The `Object.assign()` only copies the property values. – user1012181 Oct 18 '17 at 20:09
  • I agree this is not a duplicate. here is a deep-copy method i made: https://jsfiddle.net/8cyLp0os/ Inspect/Look at console.log to see output. I tried to respond earlier but had to hit 50rep before I could post. – Nick Timmer Oct 20 '17 at 21:31

0 Answers0