Given the following, how do I get id 10 back out?
function ChildListViewModel()
{
var self = this;
self.children = ko.observableArray([]);
self.children.push({id:20,name:"Jake"});
self.children.push({id:10,name:"Jake"});
self.find = function(id)
{
console.log(self.children().length);
setTimeout(function(){console.log(self.children().length);}, 500);
found = ko.utils.arrayFirst(self.children(), function(child) {
return child.id() === id;
});
console.log(found);
return found;
}
}
I want to do something like
ChildVM.find(10);
All attempts using ko.utils.arrayFirst
and ko.utils.arrayForEach
have failed me.
EDIT
This now works, see selected answers.
Issues around loading order and AJAX meant this was not working as it should do.
return ko.utils.arrayFirst(self.children(), function(child) {
return child.id === id;
});
Just remember to use self.children()
to get to the underlying array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With