All the examples I see of using the IndexOf() method in KnockoutJS are of basic string types. What I want to know is how to return the index of a array that is an object, based on one of the object variables.
标签:
knockout.js
相关问题
- implementing html5 drag and drop photos with knock
- knockout checked binding doesn't update
- Knockout JS - Binding to array of observable Ints
- Knockout.js autocomplete bindingHandler [closed]
- Javascript in Edge only works with devtools open
相关文章
- Handle IE 9 & 10's clear button with Knockout
- jQuery Chosen doesn't update select options wh
- KnockoutJS property doesn't update when changi
- knockout.js - data-bind text default value
- Setting default values for computed Observable Kno
- Mapping: foreach binding work only the first time
- Knockout radio button binding with boolean
- Ajax Post and Redirect with Model Value MVC4
An observableArray exposes a method called
indexOf
, which is a wrapper toko.utils.arrayIndexOf
that simply loops through the array looking for the item that you pass to it.So, if you have the item you can do:
If you just have something like a key, then KO does have a utility function called
ko.utils.arrayFirst
that just loops through the array trying to match the condition that you pass to it. However, it returns the item and not the index of it. It would be slightly inefficient to get the object and then call indexOf on it, as you would make two passes through the array.You could just write a loop yourself looking for the right item or write a generic function based on ko.utils.arrayFirst that would look like:
Now, you can pass an array, a condition, and you will be returned the index of the first item that matches.