Zed_Blade Zed_Blade - 3 months ago 26
Javascript Question

Lodash remove items recursively

Given this JSON object, how does lodash remove the

reach
value from the objects?

{
total: 350,
SN1: {
reach: 200,
engagementRate: 1.35
},
SN2: {
reach: 150,
engagementRate: 1.19
}
}


I've been trying to iteratively remove() it but I always get an undefined object in return so I'm positive I'm doing it wrong.
This is also the first time I'm using lodash, so that might be the actual problem.

Can anyone help?

Answer

_.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>