我有一个postgres数据库,我从中查询返回一组来自查询的对象 我正在使用的堆栈是node express,javascript,我正在使用websockets来捕获数据的传输。我正在尝试将对象编译回来 这是我从数据库中获取数据后使用的代码
`socket.on('get2GTPmatch', function(team2){
for(var i = 0; i<team2.rows.length; i++){
console.log(team2.rows[i]);
}
});'
Object {name: "Luminosity", alias: "TACO", kills: 1, deaths: 1, assists: 0}
Object {name: "Luminosity", alias: "fnx", kills: 0, deaths: 1, assists: 0}
Object {name: "Luminosity", alias: "FALLEN", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "coldzera", kills: 0, deaths: 1, assists: 0}
Object {name: "Luminosity", alias: "fer", kills: 3, deaths: 0, assists: 0}
Object {name: "Luminosity", alias: "TACO", kills: 1, deaths: 1, assists: 0}
Object {name: "Luminosity", alias: "TACO", kills: 0, deaths: 1, assists: 0}
Object {name: "Luminosity", alias: "fnx", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "FALLEN", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "coldzera", kills: 0, deaths: 1, assists: 0}
Object {name: "Luminosity", alias: "fer", kills: 3, deaths: 1, assists: 0}
Object {name: "Luminosity", alias: "TACO", kills: 0, deaths: 1, assists: 0}
Object {name: "Luminosity", alias: "TACO", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fnx", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "FALLEN", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "coldzera", kills: 1, deaths: 1, assists: 0}
Object {name: "Luminosity", alias: "fer", kills: 0, deaths: 1, assists: 0}
Object {name: "Luminosity", alias: "TACO", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fnx", kills: 2, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "FALLEN", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "coldzera", kills: 0, deaths: 1, assists: 0}
Object {name: "Luminosity", alias: "fer", kills: 1, deaths: 1, assists: 0}
Object {name: "Luminosity", alias: "TACO", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fnx", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "FALLEN", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "coldzera", kills: 1, deaths: 1, assists: 0}
Object {name: "Luminosity", alias: "fer", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fnx", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "FALLEN", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "coldzera", kills: 1, deaths: 1, assists: 0}
Object {name: "Luminosity", alias: "fer", kills: 3, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fnx", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "FALLEN", kills: 2, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "coldzera", kills: 2, deaths: 1, assists: 0}
Object {name: "Luminosity", alias: "fer", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fnx", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "FALLEN", kills: 2, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "coldzera", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fer", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fnx", kills: 2, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "FALLEN", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "coldzera", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fer", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 2, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fnx", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "FALLEN", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "coldzera", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fer", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 2, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fnx", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "FALLEN", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "coldzera", kills: 2, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fer", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fnx", kills: 2, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "FALLEN", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "coldzera", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fer", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fnx", kills: 2, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "FALLEN", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "coldzera", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fer", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 2, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fnx", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "FALLEN", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "coldzera", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fer", kills: 1, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 2, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fnx", kills: 0, deaths: 1, assists: 2}
Object {name: "Luminosity", alias: "FALLEN", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "coldzera", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fer", kills: 4, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 0, deaths: 1, assists: 1}
Object {name: "Luminosity", alias: "fnx", kills: 0, deaths: 0, assists: 0}
Object {name: "Luminosity", alias: "FALLEN", kills: 0, deaths: 0, assists: 0}
Object {name: "Luminosity", alias: "coldzera", kills: 3, deaths: 0, assists: 0}
Object {name: "Luminosity", alias: "fer", kills: 1, deaths: 0, assists: 0}
Object {name: "Luminosity", alias: "TACO", kills: 0, deaths: 0, assists: 0}
Object {name: "Luminosity", alias: "TACO", kills: 0, deaths: 0, assists: 0}
Object {name: "Luminosity", alias: "fnx", kills: 0, deaths: 0, assists: 0}
Object {name: "Luminosity", alias: "FALLEN", kills: 1, deaths: 1, assists: 0}
Object {name: "Luminosity", alias: "coldzera", kills: 2, deaths: 0, assists: 0}
Object {name: "Luminosity", alias: "fer", kills: 0, deaths: 0, assists: 0}
Object {name: "Luminosity", alias: "TACO", kills: 1, deaths: 0, assists: 0}
Object {name: "Luminosity", alias: "TACO", kills: 1, deaths: 0, assists: 0}
Object {name: "Luminosity", alias: "fnx", kills: 1, deaths: 0, assists: 0}
Object {name: "Luminosity", alias: "FALLEN", kills: 0, deaths: 1, assists: 0}
Object {name: "Luminosity", alias: "coldzera", kills: 0, deaths: 0, assists: 0}
Object {name: "Luminosity", alias: "fer", kills: 1, deaths: 0, assists: 1}
Object {name: "Luminosity", alias: "TACO", kills: 0, deaths: 0, assists: 0}
Object {name: "Luminosity", alias: "TACO", kills: 0, deaths: 0, assists: 0}
Object {name: "Luminosity", alias: "fnx", kills: 0, deaths: 0, assists: 0}
Object {name: "Luminosity", alias: "FALLEN", kills: 2, deaths: 1, assists: 0}
Object {name: "Luminosity", alias: "coldzera", kills: 0, deaths: 1, assists: 0}
有没有一种简单的方法可以将我得到的所有对象组合成一个{name:,alias:,kills :(同名/别名下的所有对象的总和),:deaths :(与杀戮相同) ),助攻:(与击杀相同)}?
感谢您的帮助。我还是个菜鸟。遗憾。
答案 0 :(得分:0)
也许使用Object.assign()方法:
var objs_merged = {};
socket.on('get2GTPmatch', function(team2){
for(var i = 0; i < team2.rows.length; i++) {
objs_merged = Object.assign(objs_merged, team2.rows[i]);
}
});
如果你想让一个对象具有求和属性,那么使用数据库查询会更容易。
答案 1 :(得分:0)
client.query(
"SELECT teams.name AS team, players.alias, SUM(kills) AS kills, SUM(deaths) AS deaths, SUM(assists) AS assists FROM rounds \
JOIN player_rounds ON rounds.id=player_rounds.round_id \
JOIN players ON player_rounds.player_id=players.steam_id \
JOIN teams ON players.team=teams.id \
WHERE rounds.game_id='B3Il5v' \
GROUP BY players.alias, teams.name\
ORDER BY teams.name",
function(err,results){
if(err) {
return console.error('error occurred');
}
// console.log(results);
socket.emit('getKDAmatch', results)
});
事实上,我遇到了SQL group by的问题。当我刚使用group by team的组时,由于某种原因会抛出错误。