首页 > 解决方案 > 如何从对象获取数据并创建对象数组?

问题描述

如您所见,在将其转换为数组并将所有这些都包含在数组中之后,我现在有多个对象我想做的是获得一个新数组,该数组应具有船长姓名以及对象中带有整数的所有分数以及所有这些对象应该像这样保存在数组中。我想要实现的目标是这样的 [{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"
    }
  }```

标签: javascriptjson

解决方案


我会说将您的数据保留为对象并执行此操作:在这里,我有 player: 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);


推荐阅读