Extend JavaScript object, and convert object into an array

in code


Turns out I use these stupid methods in a bunch of places, and the search on Github isn’t the most effective.

/**
 * Merge and extend JavaScript object.
 *
 * @param {...object} Objects to merge.
 * @return {object} object - Merged object.
 */

var extend = function() {
    var object = {};

    Array.prototype.forEach.call(arguments, function(argument) {
        argument = argument || {};

        Object.keys(argument).forEach(function(key) {
            if (!object.hasOwnProperty(key)) {
                object[key] = argument[key];
            }
        });
    });

    return object;
}
/**
 * Recursively walk an object to convert it into an array.
 *
 * @param {object} object
 * @returns {array} - Object converted to an array.
 */

var toArray = function(object) {
    return Object.keys(object).map(function(key) {
        if (object[key].toString() === '[object Object]') {
            object[key] = toArray(object[key]);
        }

        return object[key];
    });
}


A Summer of Running

in running

Achill Half Marathon

in running


Your email address will not be published. Required fields are marked *