0

I have an array called playerResults, each index is a player name, and each of those is a playerObj object. Each player has a property name and bracket.

Is there a way to search through playerResults and return all objects that have a specific bracket number?

I'm using javascript for this, with jquery.

EDIT: Here is the code:

        function showBracket(groupID) {
        //console.log(playerResults);
        var matching = playerResults.filter(function(player) {
            console.log(player.bracket);
            return player.bracket == 1; // or so
        });            

My playerResults looks like this:

console.log(playerResults)
[Tiger Woods: playerObj, Phil Mickelson: playerObj, Dustin Johnson: playerObj, Zach Johnson: playerObj, Brandt Snedeker: playerObj…]
Brandt Snedeker: playerObj
    addWin: function addWin() {
    bracket: 3
    getBracket: function getBracket() {
    getWins: function getWins() {
    name: "Brandt Snedeker"
    setBracket: function setBracket(bracketNumber) {
    wins: 0
    __proto__: playerObj
Charles Howell III: playerObj
Dustin Johnson: playerObj
Hunter Mahan: playerObj

Sorry for the mess, hopefully that makes some sense.

McB
  • 1,082
  • 1
  • 18
  • 36
  • Firstly, can you please provide some example code with your objects - code is always clearer than a description of code. Secondly you say you want to identify objects by 'group number' but haven't mentioned where this is defined. – Rory McCrossan Feb 12 '13 at 17:30
  • Yah I'm reading through that one now. I'm sure it's because I'm a bit new to this, but if I define the object manually, how to I give it all the methods I need? Right now I just create an array of playername|bracket, then explode them, set playerResults[playerName]=new playerObj(playerName,bracket) – McB Feb 12 '13 at 17:49

2 Answers2

1

What you are looking for is the filter array method:

var matching = playerResults.filter(function(player) {
    return isSpecificGroupNumber(player.name, player.bracket); // or so
});

However, this method is not supported in older browsers and may need to be shimmed if you want to support them. Or use jQuery.grep.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I tried this, and grep from searching elsewhere, but nothing is ever returned. Any suggestions for debugging it? I tried to console.log(player) within the filter function, but nothing is returned. – McB Feb 12 '13 at 17:39
  • That sounds like your `playerResults` array was empty, otherwise the callback function would be called. Could you [show](http://stackoverflow.com/posts/14838269/edit) us your code? – Bergi Feb 12 '13 at 17:41
1

You can write your own functions to do it, but I would recomment underscore.js, a library that introduces LINQ-like projection and mapping functions.

Once installed, you can use the filter function to "query" arrays:

var playerResults = [
    { name: "blah", bracket:"one"},
    { name: "blah2", bracket:"three"},
    { name: "blah3", bracket:"two"},
    { name: "blah4", bracket:"one"}
];
var bracketOne = _.filter(playerResults, function(o) {
    return o.bracket == "one";
}); 

I've put a jsFiddle together for you:

http://jsfiddle.net/ye9kd/

There are a a lot of useful functions in underscore when working with arrays and objects - _.filter() is just one of them.

Spikeh
  • 3,540
  • 4
  • 24
  • 49