可观察数组搜索的常规功能

编程入门 行业动态 更新时间:2024-10-17 02:56:26
本文介绍了可观察数组搜索的常规功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有一种方法可以执行一般功能来搜索可观察数组,如果找到的值将该对象设置为另一个可观察对象. 这个问题与 知道选择:按项目设置所选项目名称不是值

is there a way to do a general function for search an observable array and if the value found then set that object to another observable .? this question relate to Knokout select : Set Selected Item by item name not value

到目前为止,我已经尝试过

so far i have tried

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

并称为

self.IssuingcountrySelected(self.popup.getOptionByName(self.issuingCountries,'Country','Japan'))

我的可观察数组是

0: ObjectCoordinatorRegion: "EU" Country: "Australia" CountryId: 14 Id: 1 2: ObjectCoordinatorRegion: "AU" Country: "Japan" CountryId: 16 Id: 2

推荐答案

尝试类似的方法

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 get the matched object else null console.log(self.IssuingcountrySelected()) // check console };

示例工作小提琴 此处

sample working fiddle here

更多推荐

可观察数组搜索的常规功能

本文发布于:2023-11-13 02:19:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583147.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   常规   功能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!