我有一个Ajax请求,用于在差异数据库中提取数据,例如,我有2个数据库,我发出一个ajax请求,将查询将返回的那两个数据库,
[{"question_id":31,"columnheader":"joene_001","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"1.2","revenue":"2.4"},{"question_id":32,"columnheader":"joene_001_1","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"0","revenue":"0"},{"question_id":33,"columnheader":"joene_001_2","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"1.6","revenue":"3.2"},{"question_id":34,"columnheader":"TEST_001","ct_yes":"2","ct_no":"1","ct_maybe":"0","costperlead":"5","revenue":"10"},{"question_id":35,"columnheader":"TEST_001_1","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"1","revenue":"2"},{"question_id":36,"columnheader":"TEST_001_2","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"1","revenue":"2"},{"question_id":37,"columnheader":"TEST_001_3","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"2","revenue":"4"},{"question_id":38,"columnheader":"Diabetes_UK_3147","ct_yes":"4","ct_no":"1","ct_maybe":"0","costperlead":"4","revenue":"16"},{"question_id":39,"columnheader":"index_001","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"2","revenue":"4"},{"question_id":40,"columnheader":"index_001_1","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"1","revenue":"2"},{"question_id":41,"columnheader":"index_001_2","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"2","revenue":"4"},{"question_id":42,"columnheader":"index_001_3","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"3","revenue":"6"},{"question_id":43,"columnheader":"index_001_4","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"4","revenue":"8"},{"question_id":44,"columnheader":"index_001_5","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"5","revenue":"10"}]
[{"question_id":36,"columnheader":"TEST_001_2","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"1","revenue":"0"},{"question_id":37,"columnheader":"TEST_001_3","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"2","revenue":"0"},{"question_id":38,"columnheader":"Diabetes_UK_3147","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"4","revenue":"8"},{"question_id":44,"columnheader":"index_001_5","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"5","revenue":"0"},{"question_id":39,"columnheader":"index_001","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"2","revenue":"0"},{"question_id":40,"columnheader":"index_001_1","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"1","revenue":"0"},{"question_id":42,"columnheader":"index_001_3","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"3","revenue":"0"},{"question_id":41,"columnheader":"index_001_2","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"2","revenue":"0"},{"question_id":43,"columnheader":"index_001_4","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"4","revenue":"0"},{"question_id":31,"columnheader":"joene_001","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"1.2","revenue":"0"},{"question_id":32,"columnheader":"joene_001_1","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"0","revenue":"0"},{"question_id":33,"columnheader":"joene_001_2","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"1.6","revenue":"0"},{"question_id":34,"columnheader":"TEST_001","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"5","revenue":"0"},{"question_id":35,"columnheader":"TEST_001_1","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"1","revenue":"0"}]
正如您所看到的那样,有两个结果,因为我在两个数据库上查询。他们使用相同的列question_id
,columnheader
,ct_yes
,ct_no
,ct_maybe
,costperlead
,revenue
我想要做的是将所有信息与question_id
相加。例如"question_id":35
在结果1和2上都存在。我想对它的列的值求和。然后将其显示在Datatables中。到目前为止,我有代码来获取所有数据库中的记录,
$.ajax({
url: "ajax/customer/all",
type: 'GET',
success: function(result){
var myObj = $.parseJSON(result);
console.log(result);
$.each(myObj, function(key,value) {
$.ajax({
url: "ajax/callcenter/charityresponses",
type: 'GET',
data: {"from" : $("#fromDateCharityRes").val(), "to" : $("#toDateCharityRes").val(), "dbconn" : value.dbconn},
success: function(result2){
var myObj = $.parseJSON(result2);
console.log(result2);
$.each(myObj, function(key,value) {
console.log(value.question_id);
console.log(value.columnheader);
console.log(value.ct_yes);
console.log(value.ct_no);
console.log(value.ct_maybe);
console.log(value.costperlead);
console.log(value.revenue);
});
}});
});
}});
我的Ajax ajax/customer/all
获取了获取信息所需的数据库总数,ajax/callcenter/charityresponses
通过其数据库连接字符串获取有关该数据库的信息,该字符串返回上述结果。我需要的是总结相同question_id
的值,该代码应该去哪里以及我该怎么做?我已经有代码在哪里分配每个值以在数据表中呈现它。
console.log(result2);
返回
[{"question_id":31,"columnheader":"joene_001","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"1.2","revenue":"2.4"},{"question_id":32,"columnheader":"joene_001_1","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"0","revenue":"0"},{"question_id":33,"columnheader":"joene_001_2","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"1.6","revenue":"3.2"},{"question_id":34,"columnheader":"TEST_001","ct_yes":"2","ct_no":"1","ct_maybe":"0","costperlead":"5","revenue":"10"},{"question_id":35,"columnheader":"TEST_001_1","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"1","revenue":"2"},{"question_id":36,"columnheader":"TEST_001_2","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"1","revenue":"2"},{"question_id":37,"columnheader":"TEST_001_3","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"2","revenue":"4"},{"question_id":38,"columnheader":"Diabetes_UK_3147","ct_yes":"4","ct_no":"1","ct_maybe":"0","costperlead":"4","revenue":"16"},{"question_id":39,"columnheader":"index_001","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"2","revenue":"4"},{"question_id":40,"columnheader":"index_001_1","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"1","revenue":"2"},{"question_id":41,"columnheader":"index_001_2","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"2","revenue":"4"},{"question_id":42,"columnheader":"index_001_3","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"3","revenue":"6"},{"question_id":43,"columnheader":"index_001_4","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"4","revenue":"8"},{"question_id":44,"columnheader":"index_001_5","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"5","revenue":"10"}]
[{"question_id":36,"columnheader":"TEST_001_2","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"1","revenue":"0"},{"question_id":37,"columnheader":"TEST_001_3","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"2","revenue":"0"},{"question_id":38,"columnheader":"Diabetes_UK_3147","ct_yes":"2","ct_no":"0","ct_maybe":"0","costperlead":"4","revenue":"8"},{"question_id":44,"columnheader":"index_001_5","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"5","revenue":"0"},{"question_id":39,"columnheader":"index_001","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"2","revenue":"0"},{"question_id":40,"columnheader":"index_001_1","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"1","revenue":"0"},{"question_id":42,"columnheader":"index_001_3","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"3","revenue":"0"},{"question_id":41,"columnheader":"index_001_2","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"2","revenue":"0"},{"question_id":43,"columnheader":"index_001_4","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"4","revenue":"0"},{"question_id":31,"columnheader":"joene_001","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"1.2","revenue":"0"},{"question_id":32,"columnheader":"joene_001_1","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"0","revenue":"0"},{"question_id":33,"columnheader":"joene_001_2","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"1.6","revenue":"0"},{"question_id":34,"columnheader":"TEST_001","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"5","revenue":"0"},{"question_id":35,"columnheader":"TEST_001_1","ct_yes":"0","ct_no":"0","ct_maybe":"0","costperlead":"1","revenue":"0"}]
答案 0 :(得分:0)
使用jQuery的grep()
函数找到了一种方法来实现这个技巧,所以我做的是创建一个数组,然后检查question_id
是否已经存在,如果没有,请将其放入,如果是的话,更新值,所以我有
var result = $.grep(records, function(e){ return e.question_id == value.question_id; });
var records = [];
if (result.length == 0) {
records.push({
question_id: value.question_id,
columnheader: value.columnheader,
ct_yes: value.ct_yes,
ct_no: value.ct_no,
ct_maybe: value.ct_maybe,
costperlead: value.costperlead,
revenue: value.revenue,
});
} else if (result.length == 1) {
result[0].ct_yes = parseInt(result[0].ct_yes) + parseInt(value.ct_yes);
result[0].ct_no = parseInt(result[0].ct_no) + parseInt(value.ct_no);
result[0].ct_maybe = parseInt(result[0].ct_maybe) + parseInt(value.ct_maybe);
result[0].revenue = parseFloat(result[0].revenue )+ parseFloat(value.revenue);
}