Grzes Slania
Grzes Slania

Reputation: 602

Javascript - Find single instance of a property's value in an array of objects

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;
    }
} 

Upvotes: 1

Views: 109

Answers (1)

JstnPwll
JstnPwll

Reputation: 8695

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;
}

Upvotes: 2

Related Questions