I have ViewModel and one of its properties is an observableArray (receivers). I want to check if an email address already exist to the collection before adding new record to ensure that no duplicate entries will occur. Please help. I tried the approach from here How to conditionally push an item in an observable array? but it's not working. It cannot detect if item to be added with email already on the list. I want that if AddReceiver function is called I will validate the newReceiverData email address. Here's my code.
ViewModel
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
});
}
};
};