0

I need a data structure to store several JavaScript objects, and to be able to access them with a string id (get/set/delete operations).

Here is an example of the items I need to store :

var Player = function(name) {
  this.name = name;
  this.x = 0;
  this.y = 0;
  this.toString = function() {
    return 'player : ' + this.name + ' at ' + this.x + ', ' + this.y;
  };
}

I would like to store players in a data structure and to be able to get/set/delete them by their name, like players.get('Bob') to get the player with Bob as name.

At first, I thought I could use a map with the name as key (I'm using Dict from collectionsjs). But then I can't access the name from the methods of the item (toString in my example).

I could use a regular Array, keep the name attribute and implement my own get/set/delete methods, however I would rather use a reliable data structure but I can't find it.

Thanks in advance :]

Antek
  • 1,167
  • 3
  • 10
  • 18
  • This sounds to me like perfect candidate for Backbone: http://backbonejs.org/. Also, I think you should rename your `toString` method as `toString` exists already in JS objects. – 76484 Oct 08 '14 at 02:40
  • What's wrong with plain old objects? – Felix Kling Oct 08 '14 at 02:48
  • 1
    @76484: The `toString` method exist in order to allow custom objects to override them so that when it is forced into a string context (for example `'' + player`) it would generate the correct string representation instead of the generic `[Object object]`. Which is exactly what it's being used as here. – slebetman Oct 08 '14 at 04:34

3 Answers3

0

A Javascript object would work.

var players = [];
players[0] = {"name":"Bob", "age":1};
players[1] = {"name":"John", "age":4};

for (var i in players) {
    if (players[i].name == "Bob") {
        alert("Bob is " + players[i].age);
    }
}

EDIT:

var players = [];
players[0] = {"name":"Bob", "age":1};
players[1] = {"name":"John", "age":4};

players.forEach(function(player){
    if (player.name == "Bob") {
        alert("Bob is " + player.age);
    }
});
David Corbin
  • 524
  • 2
  • 11
  • 25
  • You should *not* use for in with an array: http://stackoverflow.com/a/5263872/3397771 – 76484 Oct 08 '14 at 02:16
  • Edited with foreach instead. – David Corbin Oct 08 '14 at 02:33
  • @76484—you *should not* make generalisations that do not hold for all cases. – RobG Oct 08 '14 at 02:46
  • 1
    @76484—absolutes are always unattractive. ;-) With ECMA-262 ed 3 there was a case for using *for..in* with sparse arrays, but ES5's *forEach* fixes that. There is still a case for using for..in where there is a requirement to visit all enumerable properties, which isn't common, but exists as a valid use case (though *Object.keys* removes the need to use for..in for own properties). – RobG Oct 08 '14 at 05:13
0
var Players = function(){
    this.players = [];
    this.add = function(player){
        this.players.push(player);
    }
    this.delete = function(name){
        for(var i=0;i<this.players.length;i++)
            if(this.players[i].name==name)
            {
                var f = this.players.slice(i+1,this.players.length+1);
                this.players = this.players.slice(0,i).concat(f);
                return;
            }
    }
    this.set = function(name,player){
            for(var i=0;i<this.players.length;i++)
            if(this.players[i].name==name)
            {
                this.players[i] = player;
                return;
            }
    }
    this.show = function(){
        for(var i=0;i<this.players.length;i++)
            console.log(this.players[i].toString());
    }
}

var p = new Players();
p.add(new Player('Lorem'));
p.add(new Player('Ipsum'));
p.show();
p.delete('Ipsum');
p.show();
RobG
  • 142,382
  • 31
  • 172
  • 209
Yuriy Yakym
  • 3,616
  • 17
  • 30
  • Code-only answers are not liked. You should answer the OP's question and provide an explanation of why your answer address whatever issues were in the question, or why it's preferable to other options. – RobG Oct 08 '14 at 02:43
0

What's unreliable about an array? Use the built in methods IMO. A simple example:

var players = [];

var Player = function(name) {
    this.name = name;
    this.x = 0;
    this.y = 0;
    this.toString = function() {
        return 'player : ' + this.name + ' at ' + this.x + ', ' + this.y;
    };
}

function getPlayerByName(name){
    return players.filter(function(p){
        return p.name.toLowerCase() === name.toLowerCase();
    })[0];
}

// etc...
players.push(new Player('foo'));

var fetched = getPlayerByName('foo');

console.log(fetched);

http://jsfiddle.net/cy39sqge/

Johan
  • 35,120
  • 54
  • 178
  • 293
  • I didn't mean that arrays are unreliable, but my implementation of get/set/delete/... could be. That's why I wanted an open-source library that has already been tested and optimized. I guess I will take the risk and implement it myself. – Antek Oct 09 '14 at 13:25