I'm trying to create an object, that works like an array and could have some properties that could iterate the same array:
interface ICustomer {
orderId: string
name: string
}
interface ICustomers {
[key: number]: ICustomer
findByOrderId: (id:string) => ICustomer
}
Now trying to create an example instance of ICustomers:
const customers: ICustomers = {
0: {
orderId: 'aaa',
name: "Johny"
},
1: {
orderId: 'bbb',
name: "Pablo"
},
findByOrderId: function(id: string) {
for (customer of this) {
if (customer.orderId === id) {
return customer
}
}
return null
}
}
It shows the following error: Type 'ICustomers' must have a '[Symbol.iterator]()' method that returns an iterator.(2488)
How to implement 'Symbol.iterator' in this case? Maybe there is another approach?
Here is a demo