简体   繁体   English

在javascript中将对象添加到对象内的数组

[英]Adding an object to an array within object in javascript

I'm pretty new to Javascript, but I want to create a game. 我对Java语言还很陌生,但是我想创建一个游戏。 In the game, every time a user plays or completes a certain level they'll accumulate points (sort of like an achievement or badge). 在游戏中,每当用户玩或完成一定级别的游戏时,他们都会积累积分(有点像成就或徽章)。 I need to write an algorithm that will not only keep track of the user's points, but will assign them the correct badge after they've completed an action and/or have earned the necessary points. 我需要编写一种算法,该算法不仅可以跟踪用户的积分,还可以在用户完成某项操作和/或获得必要的积分后为其分配正确的徽章。

I was thinking along the lines of creating a User object for each player that will include properties such as 'name', 'points', and an array listing the badges the player has earned so far. 我一直在考虑为每个玩家创建一个User对象,其中将包含诸如“ name”,“ points”之类的属性,以及一个列出玩家迄今获得的徽章的数组。 The badges themselves will also be objects and there will be certain properties describing the rank. 徽章本身也将是对象,并且将具有描述等级的某些属性。

Is it possible to push an object inside of another object in Javascript? 是否可以将一个对象推入Javascript中的另一个对象内? For example: 例如:

var daniel = {
  userName: "Daniel White",
  userPoints: 1250,
  userBadges = [ 'Bronze', 'Silver' ],
 }

var goldBadge = {
  badgeName: "Gold",
  minWeight: 1250,
}

Note: The 'minWeight' property is stating that the user must have a minimum of 1250 points in order to obtain that badge. 注意:“ minWeight”属性说明用户必须至少获得1250分才能获得该徽章。

Is it possible for me to assign the gold badge to user Daniel by pushing goldBadge to the array userBadges? 我是否可以通过将goldBadge推到userBadges数组来为用户Daniel分配金牌? Or am I going about it all wrong? 还是我要解决所有问题?

Is it possible for me to assign the gold badge to user Daniel by pushing goldBadge to the array userBadges? 我是否可以通过将goldBadge推到userBadges数组来为用户Daniel分配金牌? Or am I going about it all wrong? 还是我要解决所有问题?

Yeah in javascript it is possible to have an array of different types. 是的,在javascript中,可能有一系列不同类型的数组。 In this case: 在这种情况下:

var daniel = {
  userName: "Daniel White",
  userPoints: 1250,
  userBadges : ['Bronze', 'Silver']}

var goldBadge = {
  badgeName: "Gold",
  minWeight: 1250}

daniel.userBadges.push(goldBadge)

Yes you can simply push it to the userBadges property. 是的,您可以简单地pushpush送到userBadges属性。

Like this: 像这样:

var daniel = {
  userName: "Daniel White",
  userPoints: 1250,
  userBadges = [ 'Bronze', 'Silver' ],
 }


daniel.userBadges.push({
  badgeName: "Gold",
  minWeight: 1250,
})

Is it possible for me to assign the gold badge to user Daniel by pushing goldBadge to the array userBadges? 我是否可以通过将goldBadge推到userBadges数组来为用户Daniel分配金牌? Or am I going about it all wrong? 还是我要解决所有问题?

I would say...yes to both. 我会说...是的。 You totally can push gold on to badges (the other answers show how to do so), but if points are the only determinant for badges then just make a function that takes an integer and returns an array of badges: 您完全可以将金牌压在徽章上(其他答案显示了这样做的方法),但是如果点是徽章的唯一决定因素,则只需制作一个接受整数并返回徽章数组的函数即可:

const badges = [["gold", 1250], ["silver", 1000], /* etc */];
const getBadgesByScore = score => {
  return badges
    .filter(([name, thres]) => score >= thres)
    .map(([name] => name); 
};

getBadgesByScore(daniel.score); // ["gold", "silver"...

Using object properties might be conceptually simple, but you're storing a lot of duplicate data. 从概念上讲,使用对象属性可能很简单,但是您要存储大量重复数据。

Simply use push method 只需使用push方法

 var daniel = { userName: "Daniel White", userPoints: 1250, userBadges: ['Bronze', 'Silver'] } var goldBadge = { badgeName: "Gold", minWeight: 1250 } daniel.userBadges.push(goldBadge); console.log(daniel) 

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

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