如何从对象获取数据并创建对象数组?

时间:2021-04-07 11:46:25

标签: javascript json

正如您所看到的,在将其转换为数组并将所有这些放入数组后,我现在有多个对象,我想要做的是获得一个新数组,该数组应该具有队长姓名和对象中带有整数的所有分数和所有这些对象都应该像数组一样保存。 我想要实现的表示将是这样的 [{captain:'John Doe',RoundNumber:Score},{captain:'John Doe',RoundNumber:Score},{captain:'JohnDoe',RoundNumber:Score}]

这是我试图获得一些帮助或指导的结果,下面是数据

    "-MXePKlz_DH8qqrIyesI" : {
      "game_name" : "test101",
      "players_info" : {
        "captain" : "john",
        "stage_name" : "fun fin fo",
        "team_members" : "John,Jane,jaden"
      },
      "response" : "this is one response",
      "round_num" : "1",
      "score" : "1"
    },
    "-MXePV53h9yol1UcLd8v" : {
      "game_name" : "test101",
      "players_info" : {
        "captain" : "Hamza",
        "stage_name" : "dssd",
        "team_members" : "01,02,03,05"
      },
      "response" : "another test response....!!!",
      "round_num" : "1",
      "score" : "0"
    },
    "-MXeiS_NOtxh3zSsG5sy" : {
      "game_name" : "mtest",
      "players_info" : {
        "captain" : "B",
        "stage_name" : "baj",
        "team_members" : "S"
      },
      "response" : "He is just gorgeous ",
      "round_num" : "1",
      "score" : "6"
    },
    "-MXeiTyNgMrIPKy2V_GG" : {
      "game_name" : "mtest",
      "players_info" : {
        "captain" : "Mo",
        "stage_name" : "fun fin fo",
        "team_members" : "Its Just Me and myself :)"
      },
      "response" : "Thats the same joke we had last week",
      "round_num" : "1",
      "score" : "4"
    },
    "-MXeiUGSqOvJdhHh64Vl" : {
      "game_name" : "mtest",
      "players_info" : {
        "captain" : "Schmed",
        "captain_email" : "SchmedtheEd@gmail.com",
        "stage_name" : "Schmed",
        "team_members" : "Schmed, Ed, Ted, Fred"
      },
      "response" : "Beard",
      "round_num" : "1",
      "score" : "2"
    }
  }```

1 个答案:

答案 0 :(得分:1)

我会说将您的数据作为一个对象并执行以下操作: 这里我有玩家:john 列出了两次,得分为 2,第一个 1 和第二个 3。结果应该列出 John 一次,总得分为 4。

var myData = {
    "-MXePKlz_DH8qqrIyesI": {
      "game_name": "test101",
      "players_info" : {
        "captain" : "john",
        "stage_name" : "fun fin fo",
        "team_members" : "John,Jane,jaden"
      },
      "response" : "this is one response",
      "round_num" : "1",
      "score" : "1"
    },
  "-MXePKlz_DH8qqrIyes55": {
      "game_name": "test101",
      "players_info" : {
        "captain" : "john",
        "stage_name" : "fun fin fo",
        "team_members" : "John,Jane,jaden"
      },
      "response" : "this is one response",
      "round_num" : "1",
      "score" : "3"
    },
    "-MXePV53h9yol1UcLd8v" : {
      "game_name" : "test101",
      "players_info" : {
        "captain" : "Hamza",
        "stage_name" : "dssd",
        "team_members" : "01,02,03,05"
      },
      "response" : "another test response....!!!",
      "round_num" : "1",
      "score" : "0"
    },
    "-MXeiS_NOtxh3zSsG5sy" : {
      "game_name" : "mtest",
      "players_info" : {
        "captain" : "B",
        "stage_name" : "baj",
        "team_members" : "S"
      },
      "response" : "He is just gorgeous ",
      "round_num" : "1",
      "score" : "6"
    },
    "-MXeiTyNgMrIPKy2V_GG" : {
      "game_name" : "mtest",
      "players_info" : {
        "captain" : "Mo",
        "stage_name" : "fun fin fo",
        "team_members" : "Its Just Me and myself :)"
      },
      "response" : "Thats the same joke we had last week",
      "round_num" : "1",
      "score" : "4"
    },
    "-MXeiUGSqOvJdhHh64Vl" : {
      "game_name" : "mtest",
      "players_info" : {
        "captain" : "Schmed",
        "captain_email" : "SchmedtheEd@gmail.com",
        "stage_name" : "Schmed",
        "team_members" : "Schmed, Ed, Ted, Fred"
      },
      "response" : "Beard",
      "round_num" : "1",
      "score" : "2"
    }
}

var myNewArray = [];
for (const [key, value] of Object.entries(myData)) {

  let player = myNewArray.find( e => e.captain === value.players_info.captain );
    
  if (player) {
    player.RoundNumber = Number(player.RoundNumber) + Number(value.score);
  } else {
    myNewArray.push({
      captain: value.players_info.captain,
      RoundNumber: value.score
    });
  }
}

console.log(myNewArray);