I have an observableArray, I have a name "Zippy", I need to check if it is in the array. if this name exists, I need to get its type. how should I do that?
// This observable array initially contains three objects
var anotherObservableArray = ko.observableArray([
{ name: "Bungle", type: "Bear" },
{ name: "George", type: "Hippo" },
{ name: "Zippy", type: "Unknown" }
]);
ko. computed( evaluator [, targetObject, options] ) — This form supports the most common case of creating a computed observable. evaluator — A function that is used to evaluate the computed observable's current value. targetObject — If given, defines the value of this whenever KO invokes your callback functions.
An observableArray just tracks which objects it holds, and notifies listeners when objects are added or removed.
unwrapObservable(item) Looking at the code, that call basically checks to see if item is an observable. If it is, return the value(), if it's not, just return the value.
Try this, you can use ko.utils.arrayFirst
function for checking an element with your custom logic..
var name = "Zippy";
var match = ko.utils.arrayFirst(anotherObservableArray(), function(item) {
return item.name == name;
});
var type;
if(match)
type = match.type
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