1

How can I store player names and scores in a way that is easy to set, retrieve and update? Arrays, objects etc? What are the pro's and con's? Which is more extensible?

I was thinking something like

Players {
    [Dan : 500]
    [Jess: 600]
    [Elvis: 56]
}

4 Answers 4

4

Key-value stores in Javascript are objects:

var players = {
    Dan   : 500,
    Jess  : 600,
    Elvis : 56
};

players.Dan = 300;
alert(players.Jess);
Sign up to request clarification or add additional context in comments.

Comments

1
var Players = {}; 

// adding new player.
Players["Dan"] = 0; 

// push score
Players.Dan += 10; 

Comments

0

Objects have an advantage over key-value pairs in that they are extensible. You can store more attrbutes

http://www.w3schools.com/js/js_objects.asp

Comments

0

Objects would serve your purpose :

eg:

var players = { "Dan" : 500 , "Jess" : 600 , "Elvis" : 56 }

You can easily access values using key.

eg

alert(players["Dan"]);

see this example : http://jsfiddle.net/PQxHM/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.