knockout.js - General function for observable array search -


is there way general function search observable array , if value found set object observable .? question relate knokout select : set selected item item name not value

so far have tried

self.getoptionbyname = function(instance,opt,name){             console.log(instance()[x]+'."'+opt+'"');             (var x = 0; x < instance().length; x++) {                 console.log(instance()[x].opt);                 if (instance()[x].opt == name)                     return instance()[x];                 }             return null;         } 

and called

self.issuingcountryselected(self.popup.getoptionbyname(self.issuingcountries,'country','japan')) 

and observable array

0:         objectcoordinatorregion: "eu"        country: "australia"        countryid: 14        id: 1    2:         objectcoordinatorregion: "au"        country: "japan"        countryid: 16        id: 2 

try

viewmodel:

var viewmodel = function () {     var self = this;     self.issuingcountryselected = ko.observable();     self.issuingcountries = ko.observablearray(json);      var getoptionbyname = function (instance, opt, name) {         return ko.utils.arrayfirst(instance, function (item) { //returns first matched record             if (item[opt] == name) {                 return item[opt];             }         });     }      self.issuingcountryselected(getoptionbyname(self.issuingcountries(), 'country', 'japan'))     //you matched object else null     console.log(self.issuingcountryselected()) // check console  }; 

sample working fiddle here


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -