Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Knockout.js 淘汰赛,在observableArray中找到价值的最佳方法是什么_Knockout.js_Knockout 2.0 - Fatal编程技术网

Knockout.js 淘汰赛,在observableArray中找到价值的最佳方法是什么

Knockout.js 淘汰赛,在observableArray中找到价值的最佳方法是什么,knockout.js,knockout-2.0,Knockout.js,Knockout 2.0,我有一个可观察的耳环,我有一个名字“Zippy”,我需要检查它是否在hte数组中。如果此名称存在,我需要获取其类型。我该怎么做 // This observable array initially contains three objects var anotherObservableArray = ko.observableArray([ { name: "Bungle", type: "Bear" }, { name: "George", type: "Hippo" },

我有一个可观察的耳环,我有一个名字“Zippy”,我需要检查它是否在hte数组中。如果此名称存在,我需要获取其类型。我该怎么做

// This observable array initially contains three objects
var anotherObservableArray = ko.observableArray([
    { name: "Bungle", type: "Bear" },
    { name: "George", type: "Hippo" },
    { name: "Zippy", type: "Unknown" }
]);

尝试一下,您可以使用
ko.utils.arrayFirst
函数来检查自定义逻辑中的元素

var name = "Zippy";
var match = ko.utils.arrayFirst(anotherObservableArray(), function(item) {
    return item.name == name;
});

var type;

if(match)
   type = match.type

嗨,约格拉,我还有一个问题。如果要更改数组中现有项的值,应如何操作?我把我的问题贴在这里,你能看一下吗?