0

Objective: I want to count the occurrences of an id matching between arrays The code below finds the match but we need to count the result

    var arr1 =  [ 

     {
    "image" : "FF815_1.jpg",
    "id" : "NO"
  }, {
    "image" : "FF815_0.jpg",
    "id" : "NO"
  }, {
    "image" : "FF815_4.jpg",
    "id" : "NO"
  }, {
    "image" : "PIOJD_2.jpg",
   "id" : "NO"
  }, {
    "image" : "PIOJD_4.jpg",
    "id": "JD"
  } ];
  
var arr2 =  [ 

     {
    "image" : "FF815_1.jpg",
    "id" : "NO"
  }, {
    "image" : "FF815_0.jpg",
    "id" : "NO"
  }, {
    "image" : "FF815_4.jpg",
    "id" : "NO"
  }, {
    "image" : "PIOJD_2.jpg",
    "id" : "JD"
  }, {
    "image" : "PIOJD_4.jpg",
    "id" : "JD"
  } ];


 angular.forEach(arr1, function(value1, key1) {
        angular.forEach(arr2, function(value2, key2) {
            if (value1.id === value2.id) {
            console.log(value2.id);
    }
    });
  });

Heres a fiddle: https://jsfiddle.net/62s0ae7v/

1
  • Ideas: create a new array and pass results to new array and count .length Commented Jan 8, 2021 at 6:54

1 Answer 1

1

Just add a simple counter like :

var counter = 0;
angular.forEach(arr1, function(value1, key1) {
    angular.forEach(arr2, function(value2, key2) {
        if (value1.image === value2.image) {
            console.log(value2.image);
            counter++;
        }
    });
});
console.log(counter);
Sign up to request clarification or add additional context in comments.

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.