2

I have array of object and trying get the index for object which is having particular value

for ex. for simple array we can achieve this using

var numbers = [1, 2, 3, 4, 5]
let get3 = exchangeRateList.firstIndex(of: 3). // result 2

how to achieve same for array with objects (for ex. to get index of object having id 3 )

 var objarray = [Name]()
        objarray.append(Name(id: 1, name: "Nuibb"))
        objarray.append(Name(id: 2, name: "Smith"))
        objarray.append(Name(id: 3, name: "Pollock"))
        objarray.append(Name(id: 4, name: "James"))
        objarray.append(Name(id: 5, name: "Farni"))
        objarray.append(Name(id: 6, name: "Kuni"))
1

1 Answer 1

8

Use firstIndex(where:) to get that working.

let index = objarray.firstIndex { $0.id == 3 } //2
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.