jquery - Filtering through Javascript 2d array -
i have 2d array
var items = [['alex',2],['alex',4],['alex',6],['jhon',11],['jhon',13]];
but in html want render array like
**names** **numbers** alex => 2,4,6 jhon => 11,13
you can use reduce()
return object , object.keys()
, foreach
loop add html
var items = [['alex',2],['alex',4],['alex',6],['jhon',11],['jhon',13]]; var result = items.reduce(function(r, e) { r[e[0]] = (r[e[0]] || []).concat(e[1]); return r; }, {}); var rows = ''; object.keys(result).foreach(function(e) { rows += '<tr><td>' + e + '</td><td>' + result[e].join(',') + '</td></tr>'; }); document.queryselector('table tbody').innerhtml += rows;
<table> <tbody> <tr><td>names</td><td>numbers</td></tr> </tbody> </table>
Comments
Post a Comment