我想找到特定的键值是否是嵌套对象。
{
'a': {
'area': 'abc'
},
'b': {
'area': {
'city': 'aaaa',
'state': 'ggggg'
}
}
}
在上面的例子中,我想找到一个'和' b'是对象还是嵌套对象?
答案 0 :(得分:1)
如果你想知道对象中的所有键是否都包含嵌套对象,那么一种可能的解决方案是使用R.map
和R.propSatisfies
将对象的所有值转换为布尔值,表示是否嵌套属性是否为对象。
const fn = R.map(R.propSatisfies(R.is(Object), 'area'))
const example = {
'a': {
'area': 'abc'
},
'b': {
'area': {
'city': 'aaaa',
'state': 'ggggg'
}
}
}
console.log(fn(example))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
如果您只想知道对象的特定键是否包含嵌套对象,则可以使用R.prop
和R.propSatisfies
的组合来执行此操作。
const fn = R.pipe(R.prop, R.propSatisfies(R.is(Object), 'area'))
const example = {
'a': {
'area': 'abc'
},
'b': {
'area': {
'city': 'aaaa',
'state': 'ggggg'
}
}
}
console.log('a:', fn('a', example))
console.log('b:', fn('b', example))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>