-2

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.

5
  • So, the index of the _id in the array of objects is always not there. You're comparing the _id to the object; that's not going to work. Commented Oct 5, 2016 at 13:30
  • You cant check with indexOf with an object having multiple attributes.Instead iterate $scope.speakerdata and do the same check for i th element of the iteration. Commented Oct 5, 2016 at 13:31
  • If the array is an array of objects, then it only makes sense to pass indexOf an object. It will find it if you pass indexOf a 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 want Array#some. Array#find is another option if you know the users have a reasonably new browser. Commented Oct 5, 2016 at 13:35
  • Actually I am new to Angular, Can you please just elaborate more? @Angular_10 Commented Oct 5, 2016 at 13:35
  • May I suggest you use lodash or underscorejs Commented Oct 5, 2016 at 13:36

3 Answers 3

1

You may use array.find method to find a speaker by _id

if (!$scope.speakerdata.find(function(s) { return s._id === speakerdatanew._id)) {
    $scope.speakerdata.push(speakerdatanew);
    console.log($scope.speakerdata); 
} 
Sign up to request clarification or add additional context in comments.

3 Comments

Can you please elaborate?
Array.find is not supported in Internet Explorer. Array.some always works with small changes.
1

There are many possible answers for this problem statement but I 've went for simpler approach where I can perform different actions at each step of checking if the object is present or not.Below is the sample snippet for your question.

You can even use angular.forEach for iterating list

angular.forEach($scope.speakerdata, function(val, key) {

            });

But there is no facility for break or continue keywords in that so I didn't use.

 .result.then(function (speaker) { //getting response from the modal
        var speakerdatanew = {
            _id: speaker._id,
            name: speaker.name,
            email: speaker.email
        };

        var alreadyPresent = false;
        for (var i = 0; i < $scope.speakerdata.length; i++) {
            var speakerData = $scope.speakerdata[i];
            if (speakerData._id == speakerdatanew._id) {
                alreadyPresent = true;
                break;
            } else {

            }
        }
        if (alreadyPresent) {
            alert("You have already added speaker");
        } else {
            $scope.speakerdata.push(speakerdatanew);
            console.log($scope.speakerdata);
        }

Comments

1

An array of objects differs a bit. This is non AngularJS, just simple JavaScript.

 function myIndexOf(myArray, searchTerm, property) {
   for (var i = 0; i < myArray.length; i++) {
     if (myArray[i][property] === searchTerm) return i;
   }
   return -1;
 }

Usage

if(myIndexOf($scope.speakerdata, speakerdatanew , "_id")===-1){

There are other options but this is a simple example.

Note I selfishly pulled this from another answer I created that has examples of removing objects, lookup, lookupall with a link to an even more complex one. Multidimensional array vs array of objects

At some point it might serve to use a library or investigate options to these although these do work in older browsers - and I did a small bit of testing for performance in those older browsers having had to support those.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.