gpt4 book ai didi

javascript - lambda : How to remove keys in objects with empty values?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:56:33 24 4
gpt4 key购买 nike

我有这个对象:

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>

关于javascript - lambda : How to remove keys in objects with empty values?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56618149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com