如何使用javascript填充表中的json。不重复重复的id并将其附加到tr

时间:2017-08-06 05:31:07

标签: javascript json

Json格式:

data:{
  id:ID,
  user_id:USERID
}

表格格式:

我想使用javascript创建动态表,我希望表格格式如图所示: enter image description here

2 个答案:

答案 0 :(得分:0)

I highly recommend utilizing lodash.js and their uniqBy function
LoDash https://lodash.com/docs/4.17.4#uniqBy
JSFiddle https://jsfiddle.net/671qucpd/

Code

    var data = [ { "EmpId": 1, "Name": "Abc", "Status": "P", "StatusDate": "01/01/2017" }, { "EmpId": 1, "Name": "Abc", "Status": "A", "StatusDate": "01/02/2017" }, { "EmpId": 2, "Name": "Xyz", "Status": "P", "StatusDate": "01/01/2017" }, { "EmpId": 2, "Name": "Xyz", "Status": "P", "StatusDate": "01/02/2017" } ];

    console.log(data);

    var unique = _.uniqBy(data, 'EmpId');

    console.log(unique);

答案 1 :(得分:0)

使用jquery



var tempObj = [];
arry.forEach(function(obj) {
  if (tempObj.indexOf(obj.EmpId) === -1) {
    tempObj.push(obj.EmpId);
    $('#table1 tbody').append('<tr><td>' + obj.EmpId + '</td><td>' + obj.Name + '</td><td>' + obj.Status + '</td><td>' + obj.StatusDate + '</td></tr>');
  }
});
&#13;
&#13;
&#13;