简体   繁体   中英

How do i get data from object and create array of objects?

As you can see i have multiple objects now after converting this to array and having all of these inside array what i want to do is get a new array that should have captain name and all the scores with round number in a object and all of these objects should be saved like that in array. A representation of what i want to be achieved would be something like this [{captain:'John Doe',RoundNumber:Score},{captain:'John Doe',RoundNumber:Score},{captain:'JohnDoe',RoundNumber:Score}]

this is the result im trying to achieve some help or guidance would be much appreciated below is the data

    "-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"
    }
  }```

I would say keep your data as an object and do this: Here I have player: john listed twice with 2 scores, first 1 and second 3. the result should list John once with score of 4 total.

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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