Ramda:如何删除具有空值的对象中的键?

问题描述 投票:0回答:7

我有这个物体:

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
也不起作用。我做错了什么?

javascript functional-programming javascript-objects ramda.js
7个回答
11
投票

您可以使用 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>


6
投票

我就是这样做的, 但我还需要排除可为空的值,而不仅仅是空值。

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

基本上拒绝就像过滤器但不包括结果。做过滤器(不是(....),项目) 如果我的任何条件通过,它将拒绝特定的密钥。

希望有帮助!


2
投票

你可以使用纯 JavaScript 来实现这个吗? (没有拉姆达)

如果确实需要从对象中删除属性,可以使用 删除运算符

for (const key in obj) {
    if (obj[key] === "") {
        delete obj[key];
    }
}

如果您喜欢单行:

Object.entries(obj).forEach(e => {if (e[1] === "") delete obj[e[0]]});

1
投票

如果你想要一个纯javascript答案:

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

// Iterate over obj keys
const newObj = Object.keys(obj).reduce((acc, key) => ({
  // Return the accumulator obj on every iteration
  ...acc,
  // Decide if we want to return the current {key:value} pair 
  ...(obj[key] !== '' ? { [key]: obj[key] } : {}),
// Initialize the accumulator obj
}), {});

0
投票
reject(complement(identity))
({
  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
})


{"matrimonyUrl": "christian-grooms",
"religion": "Christian", 
"search_criteria": "a:2:{s:6:\"gender\";s:4:\"Male\";s:9:\"community\";s:9:\"Christian\";}"
}

0
投票

要删除未定义的值,您可以使用

isNil

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

const result = R.reject(R.isNil)(obj);

console.log(result);

-2
投票
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);
© www.soinside.com 2019 - 2024. All rights reserved.