如何在Javascript/angular中向JSON数组添加值?

如何在Javascript/angular中向JSON数组添加值?,javascript,arrays,json,angular,Javascript,Arrays,Json,Angular,我在angular应用程序中的文件中初始化了这些: names = ['A','B','C','D']; const score = [{}]; 在做了一些检查当前玩家的处理之后,我想为每个玩家创建一个数组。每个玩家的阵型会被单独添加。首先是玩家A,然后是B,依此类推,直到名称数组中的最后一个玩家被调用,因为他们从另一个页面获得分数。 那么像这样, { "A":{ "End#1":{"arrow1": "here the score",

我在angular应用程序中的文件中初始化了这些:

  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文件,它应该在这里实现。。。(),希望此文件能有所帮助如果我正确理解您的问题,JSON对象的语法应该如下所示:

 {
  "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对象,只需要一个数组。编辑以反映这一点

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];
    }
}
for(变量i=0,i
只需注意—您不会向数组添加任何新条目,您只需向对象添加一个新数组,并以“End#n”作为键


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

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

Var resObj={End#1:[“arrow1”:“这里是分数”, “箭头2”:“这是分数”, “箭头3”:“这是分数” ]} ;

乔布。A.推送(再推): 乔布。B.推送(再推): 乔布。C.推送(再推): 乔布。D.推送(再推): 乔布。E.推送(再推):


通过上述模式,您可以插入记录,也可以将记录推送到循环的状态之上,使其成为动态的

你能先修复阵列吗?对象数组语法不正确。array.push和JSON.parse应执行此操作正确的语法是什么?我是json新手,看到有人在某处使用这样的语法:/@JaromandaX@JaromandaX现在是Valid吗?@JaromandaX好的,我希望我的语法现在更好;)怎么可能创建这样一个数组,因为这是我在脑海中创建的一个数组,它们看起来应该是这样的。我不知道如何将每个箭头的值添加到正确名称的正确字段中…jobj。A.推送(resObj);乔布。B.推(再推);乔布。C.推动(resObj);乔布。D.推(再推);乔布。E.推送(resObj);请更正:带有;是的,这看起来像是我要做的。虽然我的程序是用angular编写的,但我不知道如何将值放入数组中。现在我知道它们的数组应该是什么样子了……在“End#1”行后面不应该有逗号吗??还是我弄错了?是的,编辑过。这是纯JavaScript的。没有东西是用Angular写的,Angular是用打字机写的。如果您不知道如何将其转换为Angular,我建议您在尝试学习Angular之前先学习JavaScript和TypeScript的基础知识?对我来说,它是作为未使用的局部变量出现的。。。这对angular有用吗?@ArcherMark发现得很好,修正了错误!应该在下一行中使用它,而不是
jsonObj