0

I'm writing a simple JavaScript game that uses an object to represent a set of teams with scores and other values for the current game as well as previous games. The object will look something like this:

var leaderboard = [ {team:"Black", captain:"Sparrow", score: 100, treasureFound: 500, totalTreasure: 5000, wins: 2}, {team:"Blue", captain:"Smollett", score: 5, treasureFound: 0, totalTreasure: 200, wins: 1}, {team:"Green", captain:"Hook", score: 10000, treasureFound: 4500, totalTreasure: 25000, wins: 10} ]; 

What I'd like to do is reset the score values after each game while persisting the running total values for the session. One way to do this would be with a loop, but I'm wondering if there's a cleaner way to do this without explicitly looping through each team to set all the scores to the same value of 0. Any thoughts?

Here's a fiddle for it: http://jsfiddle.net/2gbgkLg3/1/

Edit: the original question text referenced looking for a non-iterative syntax, but I think this was confusing and distracted from what I was really asking, so I altered it.

3
  • 1
    you could use a function and an array iteration method, like forEach, but a for-loop would suffice just fine. there's no easy way to get around the need for iteration, unless you use self-updating getters. Commented Jun 30, 2015 at 21:48
  • Do you have any good resources for going that route or any sample code based on the example? Thanks! Commented Jul 1, 2015 at 13:10
  • 1
    jsfiddle.net/2gbgkLg3/2 Commented Jul 1, 2015 at 18:59

1 Answer 1

1

You could use forEach (which will iterate through the original array)

leaderboard.forEach(function(record) { record.score = 0; record.treasureFound = 0; record.totalTreasure = 0; }); 

You could use a map function (which will create a new array)

leaderboard = leaderboard.map(function(record) { record.score = 0; record.treasureFound = 0; record.totalTreasure = 0; return record; }); 

So yes, both of these are iterative. But you're working with an array of data so presumably it's a variable amount of records. There's really no other way to do this.

If you know you'll always have 3 teams, you could do this

leaderboard[0].score = 0; leaderboard[0].treasureFound = 0; leaderboard[1].score = 0; leaderboard[1].treasureFound = 0; leaderboard[2].score = 0; leaderboard[2].treasureFound = 0; 

But your leaderboard will break as soon as you have more than 3 teams — the 4th team's stats will not be reset.

It's more code and looks stupid when a simple loop could be used.


A final option would be to use something like jQuery's $.extend to perform a deep merge.

$.extend(true, leaderboard, [ {score: 0, treasureFound: 0}, {score: 0, treasureFound: 0}, {score: 0, treasureFound: 0} ]); 

However, this will fail again as soon as you have more or less than 3 team entries on the leaderboard.

Sign up to request clarification or add additional context in comments.

3 Comments

these are both still iterative solutions though, correct?
@grdevphl I edited my answer to answer your question
@grdevphl yeah, SQL is a declarative language that does not make the user concern his/herself with how the select/update/etc happens. Behind the scenes, SQL is likely boiling down to some iterative update for the number of matching rows in your query. JavaScript on the other hand will require an imperative program that includes instructions on how to accomplish your task.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.