I have an array with objects. One of the objects properties is "position". Another is "player_name". There are many players and only 8 unique positions. I want to go through the array and grab the first player for each position and move it to a new array. In the end, I should have a new array with a total of 8 players, each with a unique position.
Here's my code:
// Sample Object: { "name": "Frank Johnson", "position": "C", "cnx": "17" }
function compare(a,b) {
if (parseFloat(a.cnx) < b.cnx)
return -1;
if (parseFloat(a.cnx) > b.cnx)
return 1;
return 0;
}
var sorted = array.sort(compare);
var new_array = []
sorted.forEach(function (i) {
if (i.position === "1B" ) {
new_array.push(i)
// How do I make it stop looking for 1B's at this point??
}
})
"1B". When it stops looking for that, what should it do instead?arraycreated for you to sort?