Question

I am using lodash.js and am trying to remove a key pair from the below object based on the value not the key name - note, I cannot use the key name:

var filters = {
   "filter1": "height",
   "filter2": "length",
   "filter3": "width"
}

This will remove a key pair by name

_.omit(filters, 'filter1');

Does anyone know how to removed it based upon value? Cheers

Was it helpful?

Solution

You can use _.omit with a callback:

_.omit(filters, function (value) {
    return value === 'width';
})

OTHER TIPS

The omit method accepts a callback, so you could just use this:

_.omit(filters, function(v){return v == "height";});

Demonstration

According to the docs omit it can take a call back argument that is a function that returns true for each pair that should be omitted. In that function you can do whatever crazy logic you like.

_.omit(filters, function(value, key, object) {
  return value === 'width'; // omit width
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top