问题
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 an array of objects and want to push each position into their own array.
Ex. each object has a key of "position" so any object with the value of "QB" for the key "position" should be pushed into a new array "Quarterbacks".
Here's the code so far — it's in Angular so I first save all the players to a variable "Roster" and from there need to separate players by position into separate arrays that hold all players for that position.
angular.module('clientApp')
.controller('PlayersCtrl', function (
$scope,
Player
) {
// $scope.players = Player.getList().$object;
var Roster = Player.getList().$object;
console.log(Roster);
});
Although i'm interested in learning how to do this all in one function that pushes all positions into their own array, i'd also like to figure out how to do it for one position. Like how to push all the quarterbacks from the Roster into a new array of just quarterbacks.
回答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;
}
}
}
回答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]);
}
回答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);
回答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:
function createListByPosition(inputList, desiredPosition) {
var outputList = new Array();
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.
来源:https://stackoverflow.com/questions/34095758/sort-through-an-array-of-objects-and-push-objects-with-the-same-key-value-pair-t