RedGiant
RedGiant

Reputation: 4748

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

Upvotes: 0

Views: 695

Answers (1)

Deadfish
Deadfish

Reputation: 2057

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.

Upvotes: 1

Related Questions