1

I have a function that returns an array of objects data.sportdata. I would like to get all array elements with the same sports_id. The code

$scope.arrSportData = data.sportdata;
angular.forEach($scope.arrSportData, function(value, key) {
    console.log($scope.arrSportData);
    //getting reponse
    /*
        Object { id: "1", user_id: "2", sport_id: "1", position_id: "1", team_name: "JimmyTmname",}
        Object { id: "2", user_id: "2", sport_id: "2", position_id: "6", team_name: "JimmyTmname2",}
        Object { id: "3", user_id: "2", sport_id: "3", position_id: "12", team_name: "JimmyTmname3",}
        Object { id: "4", user_id: "2", sport_id: "5", position_id: "20", team_name: "JimmyTmname5",}
    */

    //code i wrote
    if (value.sport_id == 1) {
        $scope.positionId.spr1 = value.position_id;
        $scope.teamname.spr1 = value.team_name;
    }
    if (value.sport_id == 2) {
        $scope.positionId.spr2 = value.position_id;
        $scope.teamname.spr2 = value.team_name;
    }
    if (value.sport_id == 3) {
        $scope.positionId.spr3 = value.position_id;
        $scope.teamname.spr3 = value.team_name;
    }
    if (value.sport_id == 4) {
        $scope.positionId.spr4 = value.position_id;
        $scope.teamname.spr4 = value.team_name;
    }
});

Here I am always getting first value and nothing more. Please suggest and help to solve this problem.

I did try outside of the loop but does not work. I think filter function can do this but dont know how does it work.

naveen
  • 53,448
  • 46
  • 161
  • 251
James
  • 33
  • 4

1 Answers1

0

Perform a groupBy function which will give you an object like this

{ 
    key: [],
    key: []
}

Here the key will be sport_id and [] will be the items with the same key.

A minimal working example will be,

// Written By: Ceaser Bautista
//Link: http://stackoverflow.com/a/34890276/17447
var groupBy = function(xs, key) {
  return xs.reduce(function(rv, x) {
    (rv[x[key]] = rv[x[key]] || []).push(x);
    return rv;
  }, {});
};

//here we have four teams with two sports id 13 and 22
var arrSportData = [{
  id: "1",
  user_id: "1",
  sport_id: "13",
  position_id: "1",
  team_name: "JimmyTmname"
}, {
  id: "2",
  user_id: "2",
  sport_id: "22",
  position_id: "6",
  team_name: "JimmyTmname2"
}, {
  id: "3",
  user_id: "3",
  sport_id: "22",
  position_id: "12",
  team_name: "JimmyTmname2",
}, {
  id: "4",
  user_id: "4",
  sport_id: "13",
  position_id: "20",
  team_name: "JimmyTmname1"
}];

$scope.groupedData = groupBy(arrSportData, "sport_id");
console.log(groupedData);

Now you will have an array for each sports_id. In view populate it like

<div ng-repeat="(key, items) in groupedData">
    <h4>Sports ID: {{key}}</h4>
    <ul ng-repeat="item in items">
        <li>User ID: {{item.user_id}}</li>
    </ul>
</div>
naveen
  • 53,448
  • 46
  • 161
  • 251
  • thats nice Naveen could you please suggest me how could it store those value into controller mean i don't wish to loop through ng-repeat want to use angular.forEach() method so that can store like $scope.userid= item.user_id like that – James Jul 28 '16 at 06:17
  • angular.forEach($scope.groupedData, function(value, key){ $scope.positionID = value.position_id; console.log($scope.positionID); }); I wrote but undefined says – James Jul 28 '16 at 06:20
  • I want to store user_id into $scope.userIDS variable could you help me – James Jul 28 '16 at 06:23