【问题标题】:How is it possible to add values to a JSON array in Javascript/angular?如何在 Javascript/angular 中向 JSON 数组添加值?
【发布时间】:2018-12-27 20:41:36
【问题描述】:

我在我的文件中初始化了这些,它位于一个角度应用程序中:

  names = ['A','B','C','D'];

  const score = [{}];

在做一些检查当前播放器的处理之后,我想为每个播放器创建一个数组。每个玩家的数组都会单独添加。第一个玩家 A,然后是 B,依此类推,直到名称数组中的最后一个玩家被调用,因为他们正在从另一个页面获取分数。 所以,像这样:

 {
  "A":{
       "End#1":{"arrow1": "here the score",
              "arrow2": "here the score",
              "arrow3": "here the score"
             }
      },
  "B":{
       "End#1":{"arrow1": "here the score",
              "arrow2": "here the score",
              "arrow3": "here the score"
             }
      },
  "C":{
       "End#1":{"arrow1": "here the score",
              "arrow2": "here the score",
              "arrow3": "here the score"
             }
      },
  "D":{
       "End#1":{"arrow1": "here the score",
              "arrow2": "here the score",
              "arrow3": "here the score"
             }
      }
  }

如何动态创建类似 JSON 数组的内容,以便根据名称的数量增加和减少它?另外,怎么可能说名字“A”以后会有第二个结尾,然后我怎么能输入第二个结尾?

这是实现它的整个 Angular 文件...(https://codepen.io/ArcherMArk/pen/ZVJwyQ),希望这个文件有点帮助

【问题讨论】:

  • 你能先修复阵列吗?对象数组语法不正确。
  • array.push 和 JSON.parse 应该可以完成工作
  • 正确的语法是什么?我是 json 新手,看到人们只在某处使用这样的语法:/ @JaromandaX
  • @JaromandaX 现在有效吗?
  • @JaromandaX 好的,我希望我的语法现在更好;)如何创建这样的数组,因为这是我在脑海中创建的数组,它们应该看起来像。我不知道如何将每个箭头的值添加到正确名称的正确字段中......

标签: javascript arrays json angular


【解决方案1】:

如果我正确理解你的问题,你的 JSON 对象的语法应该是这样的:

编辑:来自 cmets 中给出的额外链接,看来您根本不需要 JSON 对象,只需要一个数组。编辑以反映这一点。

scores: [
    {
        id: "A",
        endScores: {
            "End#1" : [arrowScore1, arrowScore2, arrowScore3],
            "End#2" : [arrowScore1, arrowScore2, arrowScore3]
        },
    },
    {
        id: "B",
        endScores: {
            "End#1" : [arrowScore1, arrowScore2, arrowScore3],
            "End#2" : [arrowScore1, arrowScore2, arrowScore3]
        },
    },
    {
        id: "C",
        endScores: {
            "End#1" : [arrowScore1, arrowScore2, arrowScore3],
            "End#2" : [arrowScore1, arrowScore2, arrowScore3]
        },
    },

    ...

]

你会这样做

for (var i = 0, i < scores.length; i++ ) {
    if (scores[i].id === "A") {
        scores[i].endScores["End#3"] = [newArrowScore1, newArrowScore2, newArrowScore3];
    }
}

请注意 - 您不会向数组添加任何新条目,您只是向对象添加一个新数组,其中“End#n”作为键。

请记住,如果您将“分数”设置为对象/类的属性并在该类的函数中引用它,那么您应该使用 this.scores

【讨论】:

  • 是的,这看起来像是我想要做的。我的程序是用角度编写的,我不知道如何将值放入数组中。现在我知道它们的数组应该是什么样子了……
  • 应该在“End#1”行之后没有逗号??还是我弄错了?
  • 是的,已编辑。这是在纯 JavaScript 中。没有什么是用 Angular 编写的,Angular 是用 TypeScript 编写的。如果您不知道如何将其转换为在 Angular 中工作,我建议您在尝试学习 Angular 之前学习 JavaScript 和 TypeScript 的基础知识。
  • 这一行:“var obj = JSON.parse(jsonObj);”你在哪里使用obj?对我来说,它作为未使用的局部变量出现......这应该与角度一起使用吗?
  • @ArcherMark 很好发现,修复了错误!它应该用在下一行而不是jsonObj
【解决方案2】:

首先定义json对象如下 var jobj={A:[], B:[], C:[], D:[], E:[] } ;

Var resObj={End#1:["arrow1": "这里是分数", "arrow2": "这里是分数", "arrow3": "这里是分数" ]} ;

乔布斯。 A. 推(resObj): 工作j。 B. 推(resObj): 工作j。 C. 推(resObj): 工作j。 D. 推(resObj): 工作j。 E. push(resObj) :

通过上述模式,您可以插入记录,也可以将上述语句用于推送记录以使其循环以使其动态化。

【讨论】:

  • 工作。 A. 推(resObj);工作j。 B. push(resObj) ;jobj. C. 推(resObj);工作j。 D. push(resObj) ;jobj. E. push(resObj) ;请更正:用;
猜你喜欢
  • 1970-01-01
  • 2017-08-04
  • 2013-09-28
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
  • 2020-07-02
  • 1970-01-01
  • 2020-10-25
相关资源
最近更新 更多