Ramda: Is there a way to find particular key value is nested object?

2.5k views Asked by At

I want to find particular key value is nested object or not.

{
  'a': {
    'area': 'abc'
  },
  'b': {
    'area': {
      'city': 'aaaa',
      'state': 'ggggg'
    }
  }
}

In above example, I want to find 'a' and 'b' is object or nested object?

1

There are 1 answers

0
Scott Christopher On

If you want to know whether all keys in the object contain nested objects then one possible solution is to convert all of the values of the object to boolean values using R.map and R.propSatisfies, representing whether the nested property was an object or not.

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>

If you just want to know whether a specific key of an object contains a nested object, then you can do so with a composition of R.prop and 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>