1

Using array.protoype.some I am trying to find a single instance of a property's value '' in an array of objects. If the value ''is found then variable round will be 0. Here is the codepen http://codepen.io/theMugician/pen/meNeoJ?editors=101

$scope.cells = [ { value: '', disabled: false }, 
               { value: '', disabled: false },
               { value: '' , disabled: false}, 
               { value: '' , disabled: false },
               { value: '' , disabled: false},
               { value: '', disabled: false } ,
               { value: '' , disabled: false},
               { value: '', disabled: false }, 
               { value: '' , disabled: false} ];

function hasValue(element) {
    return element === '';
}

//check if all cells are filled
for(var i = 0; i < $scope.cells.length; i++){
    if($scope.cells[i].value.some(hasValue)){
        round = 0;
    }else{
        round = 1;
    }
} 
3
  • 1
    Could you please elaborate your issue? Commented Nov 30, 2015 at 18:16
  • What's in the array? Given a sample array of values, what output would you expect? If that information is already in the pen, then please include it here, the pen is a bonus but sufficient code to understand, and answer, the question is mandatory here on Tia site, in your question. Commented Nov 30, 2015 at 18:32
  • I need to loop through every object in $scope.cells and check the property value: of each object. If there is a single instance of '' in the property value then variable round = 0 else (if there is no '' in the property value in any of the objects) then round = 1 Commented Nov 30, 2015 at 18:42

1 Answer 1

2

Array.protoype.some must be called on an array. You're calling it on $scope.cells[i].value, so it throws an error.

Try removing the for loop, and simply calling some on the array:

function hasValue(element) {
  return element.value === "";
}

if($scope.cells.some(hasValue)){
  round = 0;
}else{
  round = 1;
}
Sign up to request clarification or add additional context in comments.

1 Comment

It was simpler than I thought. I should have added the property .value in the function hasValue rather than trying to execute it with array.some which it can't.

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.