从JS对象

时间:2015-06-08 19:09:02

标签: javascript for-loop lodash

如何更改Lodash中transform()方法为此对象生成的默认密钥。它们必须从1开始,而不是从0开始。

function getHashValues(section, matchExp) {
    var picked = _.transform(section, function(result, value, key) {
        var matches = key.match(new RegExp(matchExp));
        if (matches) {
            result[matches[2]] = result[matches[2]] || {};
            result[matches[2]][matches[1]] = value;
        }
    }, {});

    // Log
    console.log(typeof picked); // object
    console.log(picked); // Object {0: Object, 1: Object, 2: Object, 3: Object, 4: Object, 5: Object, 6: Object, 7: Object, 8: Object, 9: Object}

    return picked;
}

0 个答案:

没有答案