1

I have object created by function:

$scope.checkPosRole = function(possition , posRole , posFunction) {
   var rolePos = {pos: possition, posRole: posRole, posFunction: posFunction}; 
   $scope.rolePossition.push(rolePos);
   };

The problem is that I want to in the array was only 1 object with the specified value of pos. In the case when the object is added with value pos that exists already in the array I want to swap new object with object exist already in array.

I've tried every function call scan the tables with foreach, but did not bring me desirable effect. Please help.

3

1 Answer 1

4

try this

var rolePosition = [{ id: 1}, {id: 2}, {id: 3}];

function addRolePosition(data) {
  var index = -1;
  
  for(var i = 0, i < rolePosition.length; i++) {
    if(rolePosition[i].id === data.id) {
      index = i;
    }
  }
  
  if(index > -1) {
    rolePosition[index] = data;
  } else {
    rolePosition.push(data)
  }
}

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.