let merge = (...arguments) => {
      
        // Variables
        let target = {};
      
        // Merge the object into the target object
        let merger = (obj) => {
            for (let prop in obj) {
        if (obj.hasOwnProperty(prop)) {
            if (Object.prototype.toString.call(obj[prop]) === '[object Object]') {
        // If we're doing a deep merge and the property is an object
        target[prop] = merge(target[prop], obj[prop]);
            } else {
        // Otherwise, do a regular merge
        target[prop] = obj[prop];
            }
        }
            }
        };
      
        //Loop through each object and conduct a merge
        for (let i = 0; i < arguments.length; i++) {
            merger(arguments[i]);
        }
      
        return target;
      };

      Javascript language logo
      Function to Deep Merge Objects

      Javascript Interview Cookbook

      The code creates a merge object that takes two arguments: the first is an object and the second is the target object. The merge function will loop through each property in the arguments object and, if the property is an object and the toString property on Object.prototype says the property is an object, the merge function will call toString on the target object and store the result in the target object's property called "prop". If the property is not an object, the merge function will call obj[prop] on the target object and store the result in the target object's property called "prop". Finally, the merge function returns the target object.

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.