简体   繁体   中英

getting the value/key from a dynamic map object in immutable.js

I have a dynamic immutable object and I want to know a better way to get a deep-level value. Fiddle Example

var map = {aaa:{bbb:{ccc:'ddd'}}}
map = Immutable.fromJS(map)

Suppose that I don't know the third-level key ccc , what is the best way to get its value, which in this case ddd ?

var map = {aaa:{bbb:{ccc:'ddd'}}}
map = Immutable.fromJS(map)

map.keySeq().toList().map(first=>{
    map.get(first).keySeq().toList().map(second=>{
      map.getIn([first,second]).valueSeq().toList().map(third=>{
           alert(third)
      })
  })
})

Is the above example an appropriate way to get the value ddd . I can't simply use map.getIn(['aaa','bbb','ccc']) because this map object is dynamic in my situation

As I understand, you're trying to obtain the key which has ddd value. If you're aware of the object nesting levels, you can simply flatten it.

var map = {aaa:{bbb:{ccc:'ddd'},eee:{fff:'ggg'}}};
var map = map.flatten(3);
/// {
///  ccc: "ddd",
///  fff: "ggg"
/// }

Now, simply filter the map and obtain requisite key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM