0

That's was surprise, but simple function array.IndexOf is not working.

$scope.nextProduct = function (pos, item) {
    switch (pos) {
        case 0: product = $scope.Menu[0].Breakfast
            break
        case 1: product = $scope.Menu[0].Lunch
            break
        case 2: product = $scope.Menu[0].BeforTraining
            break
        case 3: product = $scope.Menu[0].AfterTraining
            break
        case 4: product = $scope.Menu[0].Dinner
            break
        default: product = $scope.Menu[0].Breakfast
            break
    }
    var index = product.indexOf(item.Name);
    product[index - 1].IsSelect = false;
    product[index + 1].IsSelected = true;
}

indexOf return -1 but I'm completely sure that the item exist in array. What can be wrong here?enter image description here

admix
  • 1,752
  • 3
  • 22
  • 27
andrey.shedko
  • 3,128
  • 10
  • 61
  • 121

1 Answers1

2

By this expression you are searching for a string in object array.

product.indexOf(item.Name);

Instead you should run:

var res = product.filter(function(elem){
    return elem.Name == someValue
})

this returns array matching your value

Sam Marland
  • 552
  • 3
  • 24
asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84