Reputation: 2588
Using Underscore (technically Lodash). Have an object that looks like the following.
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}
};
I also have a hash of arrays that I want to "clean" out of each object:
var excludes = {
first: ['name', 'age'],
second: ['occupation'],
fourth: ['kids]
};
The idea is that each of the elements in the array will be dropped from the object that has the matching key. Meaning my data will end up like this:
{
first: {occupation: 'Welder'},
second: {name: 'Tim', kids: true},
third: {name: 'Dave', occupation: 'Electrician', age: 32},
fourth: {name: 'Matt', occupation: 'Plumber', age: 41}
};
I was originally trying:
_.map(myObj, function(obj, k) {
if(_.has(excludes, k) {
// not sure what here
}
});
I was thinking of using omit at the innermost level, but I can only drop one key at a time, not a list of keys.
Upvotes: 1
Views: 1909