-1

sorry for the lack of code — but this is a pretty straight forward javascript question.

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".

What's the right way to sort through objects and push them into a new array that would work for my scenario?

Krang
  • 235
  • 1
  • 3
  • 13
  • Possible duplicate of [Sort through an array of objects and push objects with the same key/value pair to a new array](http://stackoverflow.com/questions/34095758/sort-through-an-array-of-objects-and-push-objects-with-the-same-key-value-pair-t) – halfer Mar 06 '16 at 21:51
  • This abandoned question was copy+pasted the same day and essentially asked again. Although the other question is the duplicate, I suggest this one is closed, as it does not feature code. With regrets, downvoted: please do not duplicate questions. – halfer Mar 06 '16 at 21:53

2 Answers2

1

You can create an index object that contains all the positions you've seen so far and then accumulate an array of players for each position:

var data = [
  {position: "quarterback", name: "Bob"}, 
  {position: "center", name: "Jim"},
  {position: "quarterback", name: "Ted"}, 
];

var positions = {};
data.forEach(function(item) {
    if (!(item.position in positions)) {
        positions[item.position] = [];
    }
    positions[item.position].push(item);
});

log(positions);

function log(x) {
    var div = document.createElement("div");
    div.innerHTML = JSON.stringify(x);
    document.body.appendChild(div);
}

Or you could make this into a generic grouping function like this:

var data = [
  {position: "quarterback", name: "Bob"}, 
  {position: "center", name: "Jim"},
  {position: "quarterback", name: "Ted"}
];

function groupData(data, key) {
    var results = {};
    data.forEach(function(item) {
       console.log(item);
        var value = item[key];
        if (!(value in results)) {
            results[value] = [];
        }
        results[value].push(item);
    });
    return results;
}

log(groupData(data, "position"));

function log(x) {
    var div = document.createElement("div");
    div.innerHTML = JSON.stringify(x);
    document.body.appendChild(div);
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979
1

You can use Array.prototype.forEach:

var data = [
  { position: "quarterback", name: "Bob" }, 
  { position: "center", name: "Jim" },
  { position: "quarterback", name: "Ted" }, 
];

var dataByPositions = {};
  
data.forEach(function(x) {
  dataByPositions[x.position] = dataByPositions[x.position] || [];
  dataByPositions[x.position].push(x);
});

// Demonstration purposes only:
document.body.innerHTML = "<pre>" + JSON.stringify(dataByPositions, null, 4) + "</pre>";
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101