Lets say this is the response from my api
user = {
profile: {
name: "John",
age: 32,
links: [
{
github: {
name: "John",
url: "https://github.com/john"
},
facebook: {
name: "John",
url: "https://facebook.com/john"
}
}
]
},
posts: [
{
id: 1,
title: "First post"
},
{
id: 2,
title: "Second post"
}
]
}
and my predefined object structure is
predefinedUserObj = {
profile: {
name: "",
age: "",
links: [
{
github: {
name: "",
url: "",
},
facebook: {
name: "",
url: "",
}
}
]
},
posts: []
}
I get the user object
from my Api
and I want to compare the api response with the predefined structure.
Example 1:
If the value of user.profile.links[0].github
is null
in the api response, then I want to assign whatever the value is in the predefined structure to that the property. (in this case it is { name: "", url:" }
)
Example 2:
if the value of user.profile.links
is null
then I want to assign the predefined value/object from the predefined structure (in this case it is `
[
{
github: {
name: "",
url: "",
},
facebook: {
name: "",
url: "",
}
}
]
So, whenever the structure doesn't match I want to replace it with pre populated values from predefined structure. How can I do that? Thanks in advance.