Skip to content
Advertisement

Ramda: How to remove keys in objects with empty values?

I have this object:

let obj = {
  matrimonyUrl: 'christian-grooms',
  search_criteria:
    'a:2:{s:6:"gender";s:4:"Male";s:9:"community";s:9:"Christian";}',
  mothertongue: null,
  religion: 'Christian',
  caste: '',
  country: null
};

I need to remove all key/value pairs in this object where the value is blank i.e. ''

So the caste: '' property should be removed in the above case.

I have tried:

R.omit(R.mapObjIndexed((val, key, obj) => val === ''))(obj);

But this doesn’t do anything. reject doesn’t work either. What am I doing wrong?

Advertisement

Answer

You can use R.reject (or R.filter) to remove properties from an object using a callback:

const obj = {
  matrimonyUrl: 'christian-grooms',
  search_criteria:
    'a:2:{s:6:"gender";s:4:"Male";s:9:"community";s:9:"Christian";}',
  mothertongue: null,
  religion: 'Christian',
  caste: '',
  country: null
};

const result = R.reject(R.equals(''))(obj);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement