有谁知道如何使arrayIndexOf在下面工作?我希望计算的observable返回每个数组项的index + ". " + Name
。例如1. Item one
。现在它返回-1. Item one
self.Tweet = ko.computed(function () {
var items = self.Items().map(function (elem) {
return ko.utils.arrayIndexOf(elem.Name()) + ". " + elem.Name();
return items;
}, self);
答案 0 :(得分:1)
问题是你以错误的方式使用ko.utils.arrayIndexOf函数。 ko.utils.arrayIndexOf( Array , Object )
self.computedObject=ko.computed(function(){
var items=self.colors().map(function(elem){
return ko.utils.arrayIndexOf(self.colors(),elem)+1+"."+elem.color;
});
console.log(items);
return items;
},self);
答案 1 :(得分:0)
这就是我让arrayIndexOf工作的方式
var viewModel = function() {
var self = this;
self.suggestions = ko.observableArray([]);
self.filterText = ko.observable('');
self.filterText.subscribe(function(newValue) {});
self.suggestionsFilter = ko.computed(function() {
if (self.filterText() === '') {
return self.suggestions();
} else {
return ko.utils.arrayFilter(self.suggestions(), function(item) {
var filterResults = item.option.toLowerCase().indexOf(self.filterText().toLowerCase()) == 0;
return filterResults;
});
}
});
};

<div class="col-md-6 postcode-search" id="js-postcode-search-suggestions">
<input id="name-search" class="form-control" type="search" name="postcode" minlength="1" placeholder="Postcode or Address" data-bind="textInput: filterText" />
<div class="col-md-12" data-bind="visible: suggestions().length > 1" style="display: none">
<ul class="suggestions" data-bind="foreach: suggestionsFilter">
<li class="pad-top-bottom">
<a href="#">
<span class="option" data-bind="html: option"></span>
</a>
</li>
</ul>
</div>
</div>
&#13;