1

I have 2 arrays stored in arr1 and arr2

var arr1 = [
  {
    "status": 4,
    "id": 1
  },
  {
    "status": 2,
    "id": 2
  },
  {
    "status": 1,
    "id": 3
  }];

var arr2 = [{
    "status": 4,
    "id": 1
  },
  {
    "status": 2,
    "id": 2
  },
  {
    "status": 4,
    "id": 3
  }];

Am using angular.equals(arr1, arr2) to check 2 arrays are same but when the status changes in arr2, how can i fetch the respective id from arr2.

3
  • "when the status changes" ... please provide more details as per minimal reproducible example. Numerous ways to accomplish this depending on use case Commented Apr 1, 2017 at 17:04
  • whenever the data from backend changes that is arr2's status, i want to display the respective id's status has changed. Commented Apr 1, 2017 at 17:11
  • Where's the relevant code? Commented Apr 1, 2017 at 17:20

2 Answers 2

1

This will give you the array which contains the ids of the elements with different status value.

 var elementsChanged = arr1.map((val, idx) => {
        if (val.status !== arr2[idx].status)
          return arr2[idx].id
      })
Sign up to request clarification or add additional context in comments.

Comments

1

You need to first put arr2 in $scope and then set $watch on your data to detect the exact id change. $scope.$watch('arr2', function(newValue, oldValue){.//iterate and detect your id change..}, true);

Demo for watcher

Demo Updated with @korte map suggestion.

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.