简体   繁体   English

在所有对象上的JavaScript中设置属性的简单语法

[英]Simple syntax for setting a property in JavaScript on all objects

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. 我正在编写一个简单的JavaScript游戏,该游戏使用一个对象来代表一组具有当前游戏以及以前游戏的得分和其他值的团队。 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? 一种方法是循环,但我想知道是否有一种更清洁的方法,而无需显式循环每个团队以将所有分数设置为相同的0。有什么想法吗?

Here's a fiddle for it: http://jsfiddle.net/2gbgkLg3/1/ 这是一个小提琴: 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. 编辑 :原始问题文本引用以寻找一种非迭代语法,但是我认为这很混乱并且分散了我的真正询问,因此我对其进行了更改。

You could use forEach (which will iterate through the original array) 您可以使用forEach(它将遍历原始数组)

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) 您可以使用map函数(这将创建一个新数组)

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 如果您知道总会有3个团队,可以这样做

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. 但是,如果您拥有超过3支球队,您的排行榜就会被打破-第四支球队的数据不会被重置。

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. 最后一个选择是使用类似jQuery的$ .extend之类的东西进行深度合并。

$.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. 但是,一旦排行榜上的团队条目少于或少于3个,此操作就会再次失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM