function GetConfirmItem() { var data = []; db.transaction(function(tx) { //查询所有整改项 tx.executeSql( 'select * from ry_confirm_item where confirm_id =? ', [String(confirm_id)], function(tx, rs) { if (rs.rows.length > 0) { data = merge(rs.rows); } //console.log(data) var html = template('confirm-item', data); $(".item").html(html); mui('#scroll1').pullRefresh().endPullupToRefresh(true); }); }); }; function merge(oldData) { let list = Array.from(oldData); // 把源数据先变成目标数据的规则 var oldDataRule = []; list.forEach(el => { var oldObj = { item_id: el.item_id, item_type: el.item_type, detail_content: el.detail_content, report_id: el.report_id, rectify_item_id: el.rectify_item_id, measure_data: [], } var measureObj = { id: el.id, confirm_id: el.confirm_id, report_item_id: el.report_item_id, is_conform: el.is_conform, measure_name: el.measure_name } oldObj.measure_data.push(measureObj); oldDataRule.push(oldObj); }); /** * 先去重,后合并 * 1、源数据去重 * 2、把去重后的数据和源数据中相同name的数据合并citys */ var newData = [] var newObj = {} oldDataRule.forEach((el, i) => { if (!newObj[el.item_id]) { newData.push(el); newObj[el.item_id] = true; } else { newData.forEach(el => { if (el.item_id === oldDataRule[i].item_id) { el.measure_data = el.measure_data.concat(oldDataRule[i].measure_data); } }) } }); return newData; }