A javascript equivalent for toDictionary (c#) I made a function that should ease the making of new objects out of objects or arrays.

I extended underscore so thats why the _.

        _.dictionary = function (obj, keyIteratee, valueIteratee) {
            var newObj = new Object();
            if (Array.isArray(obj)) {
                var length = obj.length;
                for (var index = 0; index &lt; length; index++) {
                    newObj[keyIteratee(obj[index], index)] = valueIteratee(obj[index], index);
                }
            } else { //assume its an object
                var keys = Object.keys(obj);
                var length = keys.length;
                for (var index = 0; index &lt; length; index++) {
                    newObj[keyIteratee(obj[keys[index]], keys[index])] = valueIteratee(obj[keys[index]], keys[index]);
                }
            }
            return newObj;
        };


usage:

_.dictionary(inputArray, function(s) { return s.Name; }, function(s) { return s.Value; });
_.dictionary(inputObject, function(s,i) { return i; }, function(s) { return s.Inner.Value; });


Is this a good way to tackle such a thing? am i missing something?
When providing an object, I don't think it makes sense to pass the index into the callbacks. Rather, pass in the current key.

Also, I think reduce reads much clearer here:

_.dictionary = function (source, keyGenerator, valueGenerator) {
    if (Array.isArray(source)) {
        return source.reduce(function (output, value, index) {
            output[keyGenerator(value, index)] = valueGenerator(value, index);
            return output;
        }, {});
    }

    return Object.keys(source).reduce(function (output, key) {
        output[keyGenerator(source[key], key)] = valueGenerator(source[key], key);
        return output;
    }, {});
};


See it here in action: https://jsfiddle.net/e4g4p7wy/