0

I am trying to compare the items in "item" array and the copyofOpList array to retrieve the data occurrences in copyofOpList

this is my try:

var _deleteUsedElement1 = function(item) {

                    for (var i = 0; i < item.length-1; i++){      
                          for (var j = 0; j< $scope.copyofOpList.length-1; j++){
                        if (item[i].operationCode == $scope.copyofOpList[j].code) {
                        $scope.copyofOpList.splice(j, 1);

                        } } } };

 $scope.compareArrays = function() {
     ...Get data from web Service
    _deleteUsedElement1(item);
  }

the copyofOpList array has 14 elements,and the item array has 2 array but my code deletes only one occurrence (the first),so please how can I correct my code,to retrieve any occurances in the copyofOpList array comparing to the item array thanks for help

2 Answers 2

3

I'd try to avoid looping inside a loop - that's neither a very elegant nor a very efficient way to get the result you want.

Here's something more elegant and most likely more efficient:

var item = [1,2], copyofOpList = [1,2,3,4,5,6,7];

var _deleteUsedElement1 = function(item, copyofOpList) {
    return copyofOpList.filter(function(listItem) {
     return item.indexOf(listItem) === -1;
   });
  };

    copyofOpList = _deleteUsedElement1(item, copyofOpList);
    console.log(copyofOpList);
    //prints [3,4,5,6,7]
}

And since I just noticed that you're comparing object properties, here's a version that filters on matching object properties:

var item = [{opCode:1},{opCode:2}],
    copyofOpList = [{opCode:1},{opCode:2},{opCode:3},{opCode:4},{opCode:5},{opCode:6},{opCode:7}];

var _deleteUsedElement1 = function(item, copyofOpList) {
      var iOpCodes = item.map(function (i) {return i.opCode;});
      return copyofOpList.filter(function(listItem) {
        return iOpCodes.indexOf(listItem.opCode) === -1;
      });
    };

copyofOpList = _deleteUsedElement1(item, copyofOpList);
console.log(copyofOpList);
//prints [{opCode:3},{opCode:4},{opCode:5},{opCode:6},{opCode:7}]

Another benefit of doing it in this manner is that you avoid modifying your arrays while you're still operating on them, a positive effect that both JonSG and Furhan S. mentioned in their answers.

Sign up to request clarification or add additional context in comments.

4 Comments

Thats very elegant and concise. Though I would like to point out that its still looping inside a loop, because indexOf() is a loop in itself :)
With your edits, this covers my answer so I will remove it.
@FurhanS. Thanks, and you're right about the loop. I guess nested fors just hurt my eyes. There's also the question of whether the for is more or less efficient than the indexOf(). It seems that for was more efficient in the early days of indexOf(), but it also seems that the performance of indexOf() has improved and the situation may have even reversed in the interim (based on some quick googling, so don't hold me to it).
@JonSG I thought your answer added to the discussion. I edited mine just now to mention the benefit of not altering the arrays while operating on them, which you also pointed out in your excellent answer.
2

Splicing will change your array. Use a temporary buffer array for new values like this:

var _deleteUsedElement1 = function(item) {
    var _temp = [];
    for (var i = 0; i < $scope.copyofOpList.length-1; i++){      
        for (var j = 0; j< item.length-1; j++){
           if ($scope.copyofOpList[i].code != item[j].operationCode) {
               _temp.push($scope.copyofOpList[j]);
           } 
        } 
    } 
    $scope.copyofOpList = _temp;
};

3 Comments

Have you tested this? I don't think it does what the requester needs.
@Tex you were right. Didn't test the first time, hope the edited code does what is required as per my understanding. Thnx for pointing out.
Hey, check out filter(), it kind of does what you are doing with _temp

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.