1

I have an array of players, each player is an object that has a number of properties, one is "goals".

var players =    [  
       {  
          "id":"4634",
          "name":"A. Turan",
          "number":"0",
          "age":"28",
          "position":"M",
          "goals":"1"
       },
       {  
          "id":"155410",
          "name":"H. Çalhano?lu",
          "number":"0",
          "age":"21",
          "position":"A",
          "goals":"0"
       },
       {  
          "id":"4788",
          "name":"B. Y?lmaz",
          "number":"0",
          "age":"30",
          "position":"A",
          "goals":"2",
       }
    ]

I've written a function to cycle through the array and push every element that has more than '0' goals to an array, topScorers. Like so:

$scope.topScorerSearch = function() {
  var topScorers = [];
  $scope.teamDetails.squad.forEach(function(o) {
      if (o.goals > 0) {
        topScorers.push(o)
      }
    });
  return topScorers;
}

With the function called as {{topScorerSearch()}}.

This returns only players who have scored. Perfect.

However, I want to run this on other properties, which will result in a lot of repetitious code. How can I make this a general purpose function that can be executed on different properties?

I tried including the 'prop' parameter, but it didn't work:

$scope.topScorerSearch = function(prop) {
  var topScorers = [];
  $scope.teamDetails.squad.forEach(function(o) {
      if (o.prop > 0) {
        topScorers.push(o)
      }
    });
  return topScorers;
}

...and called the function like this:

{{topScorerSearch(goals)}}

Why doesn't this work? Where am I going wrong?

3
  • You should accept the answer that solves the problem so others can glance and see it is resolved (also @Sidriel gets their points). Commented May 26, 2016 at 18:55
  • I was going to... It was only 3 minutes ago, give me a sec :) Commented May 26, 2016 at 18:56
  • 1
    Patience isn't my thing ;) Commented May 26, 2016 at 19:06

1 Answer 1

2

I believe the issue is that prop will not resolve to goals because goals is being treated as a variable with a null or undefined value, making prop null or undefined.

If you use the alternative way of accessing object properties object["property"] and use the function {{topScorers("goals")}} it should work out.

Sign up to request clarification or add additional context in comments.

2 Comments

Tried it, and no joy. First I switched the dot notation to square brackets and that worked fine, but then I again tried applying the parameter to the function, and the argument to it's call, and nothing...
Sorry... my mistake. I'd applied the square brackets to the expression and the function, but didn't put the property in the function call. D'oh. Thanks for your help.

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.