使用下划线省略键数组

Omit array of keys with Underscore

使用下划线(技术上是 Lodash)。有一个如下所示的对象。

var myObj = {
    first: {name: 'John', occupation: 'Welder', age: 30},
    second: {name: 'Tim', occupation: 'A/C Repair', kids: true},
    third: {name: 'Dave', occupation: 'Electrician', age: 32},
    fourth: {name: 'Matt', occupation: 'Plumber', age: 41, kids: false}
};

我还有一个数组散列,我想从每个对象中 "clean":

var excludes = {
    first: ['name', 'age'],
    second: ['occupation'],
    fourth: ['kids]
};

想法是数组中的每个元素都将从具有匹配键的对象中删除。这意味着我的数据最终会像这样:

{
    first: {occupation: 'Welder'},
    second: {name: 'Tim', kids: true},
    third: {name: 'Dave', occupation: 'Electrician', age: 32},
    fourth: {name: 'Matt', occupation: 'Plumber', age: 41}
};

我最初尝试的是:

_.map(myObj, function(obj, k) {
    if(_.has(excludes, k) {
        // not sure what here
    }
});

我想在最内层使用 omit,但我一次只能删除一个键,而不是键列表。

实际上,_.omit可以取一个键列表:

result = _.transform(myObj, function(result, val, key) {
    result[key] = _.omit(val, excludes[key]);
});