12

我有一个 observableArray,我有一个名字“Zippy”,我需要检查它是否在数组中。如果这个名字存在,我需要得到它的类型。我该怎么做?

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

1 回答 1

24

试试这个,你可以使用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
于 2012-10-31T15:02:31.873 回答