Ramda:如何删除具有空值的对象中的关键点? [英] Ramda: How to remove keys in objects with empty values?

查看:27
本文介绍了Ramda:如何删除具有空值的对象中的关键点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此对象:

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
};

我需要删除此对象中值为空的所有键/值对,即''

因此,在上述情况下应删除caste: ''属性。

我已尝试:

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

但这不会做任何事情。reject也不起作用。我做错了什么?

推荐答案

您可以使用R.reject(或R.filter)通过回调从对象中删除属性:

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>

这篇关于Ramda:如何删除具有空值的对象中的关键点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆