Skip to content Skip to sidebar Skip to footer

Sort Through An Array Of Objects And Push Objects With The Same Key/value Pair To A New Array

I need to sort through an array of objects and push objects with the same key/value pair to a new array. The objects I'm sorting through are football players. I have all players in

Solution 1:

If you are defining a separate array for each you could use a switch statement.

var roster  = [
  {player: "Ben", position: "qb", salary: 1000},
  {player: "Brown", position: "wr", salary: 1200},
  {player: "Landry", position: "qb", salary: 800}
];

var qbArray = [];
var wrArray = [];

function parseIntoArrayByPosition() {
  for (var i = 0; i < roster.length; i++) {
    var position = roster[i].position;

    switch (position) {
      case"qb":
        qbArray.push(roster[i]);
        break;
      case"wr":
        wrArray.push(roster[i]);
        break;
    }
  }
}

Solution 2:

var positions = {};

for(var i = 0; i < Roster.length; i++) {
    var position = Roster[i].position;
    positions[position] = positions[position] || [];
    positions[position].push(Roster[i]);
}

Solution 3:

Well, you could go through it with a forEach loop, and then assign your player based on a map of positions.

In my example (excuse my football knowledge), I run through an the arrayOfObject using the forEach function (that is provided thanks to the 'use strict'; statement at the top), and then use the position property of the actual object to check if it has already an array inside the targetObject, if not, I create a new array and push the player in to it.

The log statement at the end then shows you the output. I added the mapping from position QB to QuarterBack as an extra. If the map isn't found, it would take the actual position :)

'use strict';

var arrayOfObject = [
    { name: 'player', position: 'QB' },
    { name: 'player 2', position: 'W' },
    { name: 'player 3', position: 'RW' }
], targetObject = {}, map = {
  'QB': 'QuarterBacks',
  'W': 'Wingers',
  'RW': 'RightWingers'
};


arrayOfObject.forEach(function(player) {
  var pos = player.position,
      mappedPosition = map[pos] || pos;
  if (!targetObject[mappedPosition]) {
    targetObject[mappedPosition] = [];
  }
  targetObject[mappedPosition].push(player);
});

console.log(targetObject);

Solution 4:

I might do this slightly differently, but JavaScript seems to allow you to do things in a plethora of ways. If I cannibalize the list from the answer from @dspano, I get this for my code:

functioncreateListByPosition(inputList, desiredPosition) {
    var outputList = newArray();
    for (var i = 0; i < inputList.length; i++) {
        var position = inputList[i].position;
        if (position === desiredPosition)
        {
            outputList.push(inputList[i])
        }
    }
    return outputList;
}
var quaterbackList = createListByPosition(roster, 'qb');

This also has the benefit of being pretty generic.

Post a Comment for "Sort Through An Array Of Objects And Push Objects With The Same Key/value Pair To A New Array"