I want to send the data to the array without existing one. Here is the code.
result.then(function (speaker) { //getting response from the modal
var speakerdatanew = {_id: speaker._id, name: speaker.name, email: speaker.email};
if ($scope.speakerdata.indexOf(speakerdatanew._id) === -1) {
$scope.speakerdata.push(speakerdatanew);
console.log($scope.speakerdata);
} else {
alert("You have already added speaker");
}
});
It's continously adding same data multiple times.
indexOfan object. It will find it if you passindexOfa reference to one of the items in the array. It can't be an object like one in the array, it has to be a reference to the actual object in the array. You probably wantArray#some.Array#findis another option if you know the users have a reasonably new browser.