简体   繁体   中英

How to group the objects in array by their respective group number

I've already sorted my array list and arrange it in sequence by their respective group number, but I need to group it, ( let's say by a leader and his/her member ). I want to have extra fields that if the object has no the same group number, It will be automatically tag as the leader, hence, if the object has the same group number, the first object in sequence will be tag as the leader and the other will be automatically tag as a member.

THIS IS MY CODE FOR SORTING MY ARRAY LIST AND IT'S CONSOLE/OUTPUT.

$scope.sortedTopAgent = [];
var c = 0;
var sortTopAgentByGroup = function () {
for (const [index, value] of accountArray.entries()) {
    for (const [index2, value2] of TeamArray.entries()) {
        if (value2.accountname == value) {
            c = c + 1;

            $scope.sortedTopAgent.push(value2); 

            $scope.sortedTopAgent.sort(function (a, b) {
                return a.groupNo - b.groupNo;
            });
            console.log($scope.sortedTopAgent)

            //console >>

            [{
                "name": "John",
                "groupNo": 1
            },
            {
                "name": "Chelsea",
                "groupNo": 1
            },
            {
                "name": "Mark",
                "groupNo": 1
            },
            {
                "name": "Peter",
                "groupNo": 5
            },
            {
                "name": "Arya",
                "groupNo": 5
            },
            {
                "name": "Robert",
                "groupNo": 7
            },
            {
                "name": "Mary",
                "groupNo": 8
            }
        ]
            // end of console
        }
    }
}

};

I want to have an array that will look like this:

[{
    "name": "John",
    "groupNo": 1,
    "leaderNo": 1,
    "memberNo": ""
  },
  {
    "name": "Chelsea",
    "groupNo": 1,
    "leaderNo": "",
    "memberNo": 1
  },
  {
    "name": "Mark",
    "groupNo": 1,
    "leaderNo": "",
    "memberNo": 1
  },
  {
    "name": "Peter",
    "groupNo": 5,
    "leaderNo": 2,
    "memberNo": ""
  },
  {
    "name": "Arya",
    "groupNo": 5,
    "leaderNo": "",
    "memberNo": 2
  },
  {
    "name": "Robert",
    "groupNo": 7,
    "leaderNo": 3,
    "memberNo": ""
  },
  {
    "name": "Mary",
    "groupNo": 8,
    "leaderNo": 4,
    "memberNo": ""
  }
]

You could use Array#map with a way to know if a leader has already been found or not.

Here, I'm using the second argument to Array#map as a Set , which ends up being the this value inside the map callback function. I use the size property to determine the leaderNo / groupNo if required.

You could choose any way that's convenient.

 const data = [{ "name": "John", "groupNo": 1 }, { "name": "Chelsea", "groupNo": 1 }, { "name": "Mark", "groupNo": 1 }, { "name": "Peter", "groupNo": 5 }, { "name": "Arya", "groupNo": 5 }, { "name": "Robert", "groupNo": 7 }, { "name": "Mary", "groupNo": 8 } ]; const output = data.map(function(member) { if (this.has(member.groupNo)) { // Member return { ...member, "leaderNo": "", "memberNo": this.size } } // Leader this.add(member.groupNo) return { ...member, "leaderNo": this.size, "memberNo": "" } }, new Set()) console.log(output); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM