2014-02-11 73 views
0

任何人都知道如何使arrayIndexOf在下面工作?我希望計算出來的observable能夠返回每個數組項的index + ". " + Name。例如1. Item one。現在它返回-1. Item one計算可觀察arrayIndexOf

self.Tweet = ko.computed(function() { 
    var items = self.Items().map(function (elem) { 
     return ko.utils.arrayIndexOf(elem.Name()) + ". " + elem.Name(); 
    return items; 
}, self); 

回答

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); 

Here is sample in JSFiddle

+0

啊哈 - 非常感謝。 :) – user1405195

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>