james04
james04

Reputation: 1920

Get specific object.value from list of objects kotlin

I have an entity

 data class Account (accountId, username, password, active, date...)

Now i have a list of accounts and i want to get the accountId of the account that has active = 1 (The specifications of the app ensures that there is only one account with active = 1) I tried to map the accounts with active = 1 and then i should get the accountId of the only one returned...

 val activeId = accounts.map { accountDto -> accountDto.active = 1 }[0].accountId

This should work but it doesn t. How can i do it efficiently??

Upvotes: 1

Views: 2770

Answers (2)

gevondov
gevondov

Reputation: 166

You can use accounts.find { it.active == 1 }?.accountId

Upvotes: 3

Piyush
Piyush

Reputation: 193

use filter to get the account with active == 1

Upvotes: 2

Related Questions