Q:

How to add objects dynamically based on pre defined structure, if the object doesn't exists or null?

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.

A:

You can use a recursive function to check predefined key exists in object. If object key is undefined, empty or null, assign the predefined key value to the object:

function object_filter_recursive(obj, predefined) {

    if ( typeof(predefined) == "object") {
 Object.keys(predefined).forEach(function (k){
            if (typeof obj[k] !== 'undefined' && obj[k] != '') {
                if ( typeof(predefined[k]) == "object") {
                    return object_filter_recursive(obj[k], predefined[k]);
                } else {
                    predefined[k] = obj[k];
                }
             }
        })
    }

    return predefined;
}

var predefined = {
    profile: {
        name: "default name",
        age: "", // if no default value, just leave it empty //
        links: [
            {
                github: {
                    name: "default github",
                    url: "default github link",
                },
                facebook: {
                    name: "default facebook name",
                    url: "default facebook link",
                }
            }
        ]
    },
    posts: ""
}

var obj = {
    profile: {
        name: "john",
        age: "40",
        links: [
            {
                github: {
                    name: "",
                    url: "",
                },
                facebook: {
                    name: "",
                    url: "",
                }
            }
        ]
    },
    posts: [
     {something1: "value1"},
        {something2: "value2"}
    ]
}


obj = object_filter_recursive(obj, predefined);
console.log(obj);

