检查 Immutable.js 对象中深层值的最佳方法是什么?

What is the best way to check deep values in Immutable.js objects?

如何检查不可变对象中的 name 值?

var A = new Immutable.fromJS({
  b: {
   b2: {
    b21: {
     name: 'ok'
    }
  }
 }
})

我只有这个解决方案,这个看起来不太好。:

if (A.has('b') && A.get('b').has('b2') && A.get('b').get('b2').has('b21')) {
  console.log(A.get('b').get('b2').get('b21').get('name'))
}

您可以使用 hasIn 和 getIn 方法进行检查

if(A.hasIn(["b", "b2", "b21", "name"])) {
    console.log(A.getIn(["b", "b2", "b21", "name"]))
}