javascript - 如何从对象获取数据并创建对象数组?
问题描述
如您所见,在将其转换为数组并将所有这些都包含在数组中之后,我现在有多个对象我想做的是获得一个新数组,该数组应具有船长姓名以及对象中带有整数的所有分数以及所有这些对象应该像这样保存在数组中。我想要实现的目标是这样的
[{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"
}
}```
解决方案
我会说将您的数据保留为对象并执行此操作:在这里,我有 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);
推荐阅读
- firebase - 使用flutter从firestore中检索数据,其中每个文档具有不同数量的字段
- chatbot - Dialogflow CX:是否可以在页面的条件部分验证意图参数的值?
- merge - 标题未在视图中合并 - 报表生成器
- python - 列表理解中的多个循环和值分配
- javascript - 在我的代码上创建无限循环的问题在哪里?
- reactjs - React:如何使用遍历的搜索值之一填充输入字段?
- lua - 使用 Lua 检索 SNMP 变量值(oid)?
- ansible - Ansible playbook delegate_to 来自保险库的未定义变量
- excel - 使宏适应任何范围
- mongoose - 猫鼬查询子文档数组,其中包含需要使用半径过滤的位置