Lodash remove items recursively
Solution 1:
_.transform()
the object to another object, and while passing the values to the new object, check if the value is an object and if it has the 'reach' property, and if so use _.omit()
to get a new object without reach
:
var obj = {
total: 350,
SN1: {
reach: 200,
engagementRate: 1.35
},
SN2: {
reach: 150,
engagementRate: 1.19
}
};
var result = _.transform(obj, function(result, value, key) {
result[key] = _.isObject(value) && `reach` in value ? _.omit(value, 'reach') : value;
});
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
And if you need a recursive solution that will handle multiple nested object levels, here is deepOmit
, which uses the same idea, but without _.omit
, and can be used to remove multiple keys (see comments in code):
var obj = {
total: 350,
SN1: {
reach: 200,
engagementRate: 1.35,
DEEP_SN1: {
reach: 200,
engagementRate: 1.35
}
},
SN2: {
reach: 150,
engagementRate: 1.19
}
};
function deepOmit(obj, keysToOmit) {
var keysToOmitIndex = _.keyBy(Array.isArray(keysToOmit) ? keysToOmit : [keysToOmit] ); // create an index object of the keys that should be omitted
function omitFromObject(obj) { // the inner function which will be called recursivley
return _.transform(obj, function(result, value, key) { // transform to a new object
if (key in keysToOmitIndex) { // if the key is in the index skip it
return;
}
result[key] = _.isObject(value) ? omitFromObject(value) : value; // if the key is an object run it through the inner function - omitFromObject
})
}
return omitFromObject(obj); // return the inner function result
}
console.log(deepOmit(obj, 'reach')); // you can use a string for a single value
console.log(deepOmit(obj, ['reach', 'engagementRate'])); // you can use an array of strings for multiple values
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
Solution 2:
There doesn't seem to be a deep omit, but you could iterate over all the keys in the object, and delete reach
from the nested objects, recursively:
function omitDeep(obj) {
_.forIn(obj, function(value, key) {
if (_.isObject(value)) {
omitDeep(value);
} else if (key === 'reach') {
delete obj[key];
}
});
}
var obj = {
total: 350,
SN1: {
reach: 200,
engagementRate: 1.35
},
SN2: {
reach: 150,
engagementRate: 1.19
}
};
function omitDeep(obj) {
_.forIn(obj, function(value, key) {
if (_.isObject(value)) {
omitDeep(value);
} else if (key === 'reach') {
delete obj[key];
}
});
}
omitDeep(obj)
console.log(obj);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
Solution 3:
_.mapValues(object, v => _.isObject(v)? _.omit(v, 'reach'): v)
_.mapValues
(object, [iteratee=_.identity])
Creates an object with the same keys as object and values generated by running each own enumerable string keyed property of
object
thruiteratee
. The iteratee is invoked with three arguments: (value, key, object).
_.omit
(object, [props])
creates an object composed of the own and inherited enumerable string keyed properties of
object
that are not omitted.
Solution 4:
use _.mixin to extend a omitDeep method:
_.mixin({
'omitDeep': function(obj, predicate) {
return _.transform(obj, function(result, value, key) {
if (_.isObject(value)) {
value = _.omitDeep(value, predicate);
}
var doOmit = predicate(value, key);
if (!doOmit) {
_.isArray(obj) ? result.push(value) : result[key] = value;
}
});
}
});
var my = {
"key1": {
"key2": {
"key3": [null, {
"key4": "string",
"key5": true,
"key6": null,
"key7": 8,
"key7": undefined
}, null]
}
}
};
console.log(my);
console.log("omit null:", _.omitDeep(my, _.isNull));
console.log("omit undefined:", _.omitDeep(my, _.isUndefined));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>