Home > Back-end >  Accessing array that is in Map
Accessing array that is in Map

Time:11-28

what i want to achieve is check if data in map is an array and if yes get the first value from the array

Here you can see I have couple of arrays in Map and I want to access them and get the first value.

enter image description here

What I did so far is just loop through the map so I can print it.

  val map : MutableMap<String, Any>? = document.data?.toSortedMap(compareBy<String> { it.length }.thenBy { it })

  map?.forEach {
       Log.d("test", it.toString())
  }

CodePudding user response:

Not sure what is your end goal but this should do the trick

  map?.forEach { (key, value) ->
       if(value is Array<*>){
          //value.get(0) //mby also should add check if array is not empty before getting the value
       }
       Log.d("test", it.toString())
  }
  • Related