javascript - Combination of JSON values to different HTML tables -
i have json file.
["plat1","chan1","group1","a","cat1","a","a",26,1000], ["plat1","chan2","group1","a","cat2","a","a",70,14], ["plat1","chan1","group1","a","cat1","a","a",14,1000], ["plat1","chan1","group2","a","cat1","a","a",50,2000], ["plat1","chan1","group2","a","cat2","a","a",30,2000], ["plat1","chan1","group2","a","cat1","a","a",30,2000]
each object has 3 basic columns , 1 column number. want rows merge depending on combination of 3 columns , return sum of numeric column. combinations following:
plat1, group1, cat1 plat1, group1, cat2 plat1, group2, cat1 plat1, group2, cat2
etc
i trying present each case (combination) in different html tables code seems fail if replace regex '=='.
var data = { "headers":["plat","chan","group","random","cat","random","random","num","num2"], "rows":[ ["plat1","chan1","group1","a","cat1","a","a",26,1000], ["plat1","chan2","group1","a","cat2","a","a",70,14], ["plat1","chan1","group1","a","cat1","a","a",14,1000], ["plat1","chan1","group2","a","cat1","a","a",50,2000], ["plat1","chan1","group2","a","cat2","a","a",30,2000], ["plat1","chan1","group2","a","cat1","a","a",30,2000] ] }; function transformdata(rows) { const rowmap = new map(), result = []; // iterate on rows. rows.foreach(row => { const // create key, first elements joined together. key = row.slice(0,4).join(); // check if map has generated key... if (rowmap.has(key)) { // map has key, need add values const // value current key. storedrow = rowmap.get(key); // add value of current row row in map. storedrow[7] += row[7]; storedrow[8] += row[8]; } else { // key doens't exist yet, add row map. rowmap.set(key, row); } }); // iterate on entries in map , push each value // summed value array. rowmap.foreach(value => { result.push(value); }); //first table - group1 views var table1 = '<div class="row"><div class="col-lg-6" style="background-color: #e90649; width: 117px;"> </div><div class="col-lg-6" style="max-width: 100px; padding-left: 10px; font-size: 2vw;">group1<br/><br/><span style="font-size: 1vw;">cat1</span></div><div class="col-lg-6"><div class="container"><table><thead><tr><th>plat</th><th>chan</th><th>group</th><th>num</th></tr></thead><tbody>'; for (i = 0; < result.length; i++) { platformjson = json.stringify(result[i][0]); objjson = json.stringify(data.rows[i][2]); ratejson = json.stringify(data.rows[i][4]); if(platformjson.match(/plat1/) && objjson.match(/group1/) && ratejson.match(/cat1/)) { table1 +="<tr><td>" + result[i][0] + "</td><td>" + result[i][1] +"</td><td>" + result[i][2] + "</td><td>" + result[i][7] + "</td><td>" + "</td></tr>"; } } table1 += '</tbody></table></div></div></div>'; $("#three").html(table1); // return array. return result; } data.rows = transformdata(data.rows); const generatehtml = createtable(data); $("#one").html(generatehtml);
@import url('https://fonts.googleapis.com/css?family=roboto'); body { margin: 0; color:#fff; font-family: roboto; } .row { display: table; width: 100%; height: 241px; background-color:#454545; } .row > .col-lg-6 { display: table-cell; vertical-align: middle; } .container { /*display: flex;*/ flex-wrap: wrap; } .container > div { padding: 15px; margin: 5px; flex: 0 0 calc(100% - 20px); text-align: left; } /*img { padding-left: 7%; max-height:55px; width:auto; }*/ td{ padding: 2px 2px; text-align: center; margin: 6px 0; border: none; } table{ width: 100%; background-color:#454545; font-weight:500; border-collapse: separate; border-spacing:0.3em 1.1em; color: #fff; border: 0; } tr{ font-size: 1.5em; text-transform:capitalize; } tr:nth-child(1) { color: #ccc; font-size: 1.5em; } #one,#two,#three,#four{ padding-top:2%; }
<script src="https://code.jquery.com/jquery-1.9.1.js"></script> <div id="three"></div>
i can see row contains results group2 should excluded after if statement.
the wrong part here:
platformjson = json.stringify(result[i][0]); objjson = json.stringify(data.rows[i][2]); ratejson = json.stringify(data.rows[i][4]);
and has replaced with:
platformjson = json.stringify(result[i][0]); objjson = json.stringify(result[i][2]); ratejson = json.stringify(result[i][4]);
and key had extended cover more fields:
key = row.slice(0,5).join();
Comments
Post a Comment