I have an array of custom objects, having ID values as properties. Now I have another array of selected objects (selected by IDs). How can I get the indices of the first array by filtering for the selected IDs (second array)?
I would like to avoid looping and think of a solution using compactMap
or similar.
struct MyObject {
var ID: Int
var name: String
}
let myObjects = [
MyObject(ID: 3, name: "AAAA"),
MyObject(ID: 5, name: "BBBB"),
MyObject(ID: 12, name: "CCCC"),
MyObject(ID: 15, name: "DDDD"),
MyObject(ID: 20, name: "EEEE"),
MyObject(ID: 21, name: "GGGG"),
MyObject(ID: 22, name: "HHHH"),
MyObject(ID: 23, name: "IIII"),
]
let selectedIds = [5, 20, 23]
// How to get an array of indices for selectedIds
// Expected result [1, 4, 7]