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?
Ramda is generally a better approach for functional programming as it was designed for this and has a community established in this sense. Lodash is generally better otherwise when needing specific functions (esp. debounce ).
Yep. Ramda is an excellent library for getting started on thinking functionally with JavaScript. Ramda provides a great toolbox with a lot of useful functionality and decent documentation. If you'd like to try the functionality as we go, check out Ramda's REPL.
compose FunctionPerforms right-to-left function composition. The rightmost function may have any arity; the remaining functions must be unary. See also pipe.
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>
I did it like that, but I was also needed to exclude the nullable values, not only the empty one.
const obj = { a: null, b: '', c: 'hello world' };
const newObj = R.reject(R.anyPass([R.isEmpty, R.isNil]))(obj);
< --- only C going to display after
newObj = { c: 'hello world' }
Basically Reject is like filter but not include the results. doing filter(not(....), items) If any of my conditions pass it will reject the specific key.
Hope It's helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With