gpt4 book ai didi

Knockout.js 查找电子邮件地址(如果已存在于 observablearray 中)

转载 作者:行者123 更新时间:2023-12-04 02:55:06 24 4
gpt4 key购买 nike

我有 ViewModel,它的属性之一是 observableArray(接收器)。我想在添加新记录之前检查集合中是否已经存在电子邮件地址,以确保不会出现重复条目​​。请帮忙。我尝试了这里的方法 How to conditionally push an item in an observable array?但它不工作。它无法检测要添加的项目是否已在列表中。我希望如果调用 AddReceiver 函数,我将验证 newReceiverData 电子邮件地址。这是我的代码。

View 模型

var ReceiversViewModel = function () {
var self = this;
var errorModal = {};

self.firstname = ko.observable();
self.lastname = ko.observable();
self.receivers = ko.observableArray();
self.newReceiver = {
receiverfirstname: ko.observable(''),
receiverlastname: ko.observable(''),
receiveremailaddress: ko.observable('')
};
self.AddReceiver = function () {
var newReceiverData = ko.toJS(self.newReceiver);
if (ReceiverValidate() == true) {
//check if newReceiverData.receiveremailaddress value already exist
self.receivers.push({
EmailAddress: newReceiverData.receiveremailaddress,
FirstName: newReceiverData.receiverfirstname,
LastName: newReceiverData.receiverlastname
});
}
};
};

最佳答案

我通常在轻量级 underscore.js 中使用“any”函数当我使用 knockout 时,库用于此类事情。

if (!_.any(self.receivers(), function(receiver) { return receiver.receiveremailaddress() == newReceiver.receiveremailaddress(); })) {
// push new receiever
}

您可以根据需要使比较功能简单或复杂。

关于Knockout.js 查找电子邮件地址(如果已存在于 observablearray 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16809563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com