I have this object:
JavaScript
x
11
11
1
let obj = {
2
matrimonyUrl: 'christian-grooms',
3
search_criteria:
4
'a:2:{s:6:"gender";s:4:"Male";s:9:"community";s:9:"Christian";}',
5
mothertongue: null,
6
religion: 'Christian',
7
caste: '',
8
country: null
9
};
10
11
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:
JavaScript
1
2
1
R.omit(R.mapObjIndexed((val, key, obj) => val === ''))(obj);
2
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:
JavaScript
1
13
13
1
const obj = {
2
matrimonyUrl: 'christian-grooms',
3
search_criteria:
4
'a:2:{s:6:"gender";s:4:"Male";s:9:"community";s:9:"Christian";}',
5
mothertongue: null,
6
religion: 'Christian',
7
caste: '',
8
country: null
9
};
10
11
const result = R.reject(R.equals(''))(obj);
12
13
console.log(result);
JavaScript
1
1
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>