What would be the best way to remove an item from an observable array which is an item of another observable array itself? Let me provide an example (simplified).
Let’s say I have an observable array ‘chats’:
self.chats = ko.observableArray();
this is my chat object:
function chat(id, name, members) {
this.id = id;
this.name = name;
this.members = ko.observableArray(members);
}
and here is the members object:
function member(id, username) {
this.id = id;
this.username = username;
}
I want to remove a member from chats array but I know only the id of a member and I don’t know anything about a chat it belongs to. Is there any better way than looping through all chat items (using ko.utils.arrayForEach) and than all member items and check against the id?
You can use a map to map the member ids to any chats that they are in. IE:
Then you’ll have cached references to the chat objects so you can search those member lists directly.
Update
Since there is apparently a 1-1 member to chat relationship the membermap code can just look like this