我有一组对象如下:
{"id":136073,"date":"2016-12-13 16:06:55","user":"test1","expt":"ex3","subj":"1481645153775","cond":"Male,Yes","stim":"Peace","resp":0}
{"id":136075,"date":"2016-12-13 16:07:04","user":"test1","expt":"ex3","subj":"1481645035859","cond":"Male,Yes","stim":"Marriage","resp":0}
{"id":136077,"date":"2016-12-13 16:09:55","user":"test1","expt":"ex3","subj":"1481645377779","cond":"Male,Yes","stim":"Alone","resp":0}
{"id":136079,"date":"2016-12-13 16:11:41","user":"test1","expt":"ex3","subj":"1481645377779","cond":"Male,Yes","stim":"Peace","resp":0}
{"id":136145,"date":"2016-12-13 18:49:22","user":"test1","expt":"ex3","subj":"1481654705832","cond":"Male,No","stim":"Alone","resp":-2}
{"id":136147,"date":"2016-12-13 18:49:36","user":"test1","expt":"ex3","subj":"1481654705832","cond":"Male,No","stim":"Peace","resp":0}
{"id":136771,"date":"2016-12-13 22:19:01","user":"test1","expt":"ex3","subj":"1481667464470","cond":"Male,Yes","stim":"Alone","resp":2}
我想将它们分组为一个名为results
的数组。
我想为具有相同resp
属性值的对象聚合subj
属性的值。
到目前为止,我所要做的就是创建一个独特的主题列表来查找,我想将它们存储在它们旁边。
for (var i=0;i<results.length;i++) {
// console.log(JSON.stringify(results[i].subj))
if (i>0 && test.includes(results[i].subj)){
} else {
var total = test.push(results[i].subj)
// var total2 = test.push(results.resp)
// console.log(JSON.stringify(test))
}
}
console.log(test)
我有一系列的主题。
我该怎么做
1-找到所有相应的resp's
2-总结他们
3-将它们存放在主题旁边?
答案 0 :(得分:0)
为结果创建{}
,其中键是主题名称,值是总和。然后列举您的列表并总结它们。使用(res[each['subj']] || 0)
来捕获新密钥的情况。
var res = {};
list.forEach(function(each) {
res[each['subj']] = (res[each['subj']] || 0) + each['resp'];
});
console.log(res);
打印
{
"1481645153775": 0,
"1481645035859": 0,
"1481645377779": 0,
"1481654705832": -2,
"1481667464470": 2
}
答案 1 :(得分:0)
您可以使用哈希表对结果进行分组。
var results = [{ id: 136073, date: "2016-12-13 16:06:55", user: "test1", expt: "ex3", subj: "1481645153775", cond: "Male,Yes", stim: "Peace", resp: 0 }, { id: 136075, date: "2016-12-13 16:07:04", user: "test1", expt: "ex3", subj: "1481645035859", cond: "Male,Yes", stim: "Marriage", resp: 0 }, { id: 136077, date: "2016-12-13 16:09:55", user: "test1", expt: "ex3", subj: "1481645377779", cond: "Male,Yes", stim: "Alone", resp: 0 }, { id: 136079, date: "2016-12-13 16:11:41", user: "test1", expt: "ex3", subj: "1481645377779", cond: "Male,Yes", stim: "Peace", resp: 0 }, { id: 136145, date: "2016-12-13 18:49:22", user: "test1", expt: "ex3", subj: "1481654705832", cond: "Male,No", stim: "Alone", resp: -2 }, { id: 136147, date: "2016-12-13 18:49:36", user: "test1", expt: "ex3", subj: "1481654705832", cond: "Male,No", stim: "Peace", resp: 0 }, { id: 136771, date: "2016-12-13 22:19:01", user: "test1", expt: "ex3", subj: "1481667464470", cond: "Male,Yes", stim: "Alone", resp: 2 }],
grouped = [];
results.forEach(function (a) {
if (!this[a.subj]) {
this[a.subj] = { subj: a.subj, resp: 0 };
grouped.push(this[a.subj]);
}
this[a.subj].resp += a.resp;
}, Object.create(null));
console.log(grouped);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;