使用underscore.js _.each函数获取重复项?

时间:2013-02-12 11:41:38

标签: javascript html json html-table underscore.js

我正在做这样的事情来获得下划线模板中的JSON

 <% _.each(CList, function(d) {
    var i = 0 
      _.each(d, function(Cdata) {
          console.log(Cdata)
         var pid=JSON.stringify(Cdata["_id"]);
         if(typeof(Cdata["LOCATION"]) !== "undefined"){
       %>
       <tr>
       <td><input type="checkbox" id="Check<%= i %>" onclick='myfunctioncustomer(this,id,"<%= Cdata['COMPANY'] %>"," <%= Cdata['FIRST NAME'] %>","<%= Cdata['LAST NAME'] %> ","<%= Cdata['COMPANY URL'] %> ","<%= Cdata['LINKEDIN URL'] %> ","<%= Cdata['PERSONAL URL'] %> ","<%= Cdata['TWITTER URL'] %> ","<%= Cdata['DESIGNATION'] %> ","<%= Cdata['LOCATION'] %> ","<%= Cdata['PHONE'] %> ",<%= pid %>)' value='<%= Cdata["EMAIL"]  %>' ></input></td>
       <td> <%= Cdata['COMPANY'] %> </td>
       <td> <%= Cdata['FIRST NAME'] +' ' +Cdata['LAST NAME'] %> </td>
       <td> <%= Cdata['LOCATION'] %></td>
       </tr>
      <% i++; 
      }
    })%>
  <% }); %>

结果给了我json作为一个表,其中2行具有相同的值。 我得到这样的东西

 Company Name                    Name            Location
 Mercury Interactive Germany     Georg Goller    Germany
 Mercury Interactive Germany     Georg Goller    Germany
 Mercury Interactive GmbH        Gunter Kraft    Germany
 Mercury Interactive GmbH        Gunter Kraft    Germany

我在这里做错了什么.. 我想摆脱重复

我的JSON的结构:

       [{"SECTOR": "", "CITY": "Missassauga", "DESIGNATION": "ASCM II", "FIRST NAME":            "Michael", "LAST NAME": "Gambarotto", "COMPANY": "Mercury Interactive Corporation", "URL": "", "PHONE": "(416) 605-7872", "LOCATION": "Canada", "ADDRESS": "5800 Explorer Drive, Suite 320Missassauga, Ontario L4W5K9Canada", "_id": {"$oid": "50b5da3dea01a32302a6a2ae"}, "EMAIL": "mgambarotto@mercury.com", "SIZE": ""}, {"SECTOR": "", "CITY": "Missassauga", "DESIGNATION": "APM Major Account Manager", "FIRST NAME": "Tim", "LAST NAME": "Healey", "COMPANY": "Mercury Interactive Corporation", "URL": "", "PHONE": "(416) 706-0171", "LOCATION": "Canada", "ADDRESS": "5800 Explorer Drive, Suite 320Missassauga, Ontario L4W5K9Canada", "_id": {"$oid": "50b5da3dea01a32302a6abfc"}, "EMAIL": "thealey@mercury.com", "SIZE": ""}, {"SECTOR": "", "CITY": "Mississauga", "DESIGNATION": "AM Manager", "FIRST NAME": "Warren", "LAST NAME": "Borthwick", "COMPANY": "Mercury Interactive Corporate", "URL": "", "PHONE": "416-419-4370", "LOCATION": "Canada", "ADDRESS": "5060 Spectrum WaySuite 400 ON L4W 5N5MississaugaCanada", "_id": {"$oid": "50b5da3dea01a32302a6adbf"}, "EMAIL": "wborthwick@mercury.com", "SIZE": ""}]

1 个答案:

答案 0 :(得分:1)

你最内心的_.each似乎是不必要的:

  <% _.each(CList, function(Cdata, i) {
    var pid=JSON.stringify(Cdata["_id"]);
    if(typeof(Cdata["LOCATION"]) !== "undefined"){
      %>
      <tr>
      <td><input type="checkbox" id="Check<%= i %>" onclick='myfunctioncustomer(this,id,"<%= Cdata['COMPANY'] %>"," <%= Cdata['FIRST NAME'] %>","<%= Cdata['LAST NAME'] %> ","<%= Cdata['COMPANY URL'] %> ","<%= Cdata['LINKEDIN URL'] %> ","<%= Cdata['PERSONAL URL'] %> ","<%= Cdata['TWITTER URL'] %> ","<%= Cdata['DESIGNATION'] %> ","<%= Cdata['LOCATION'] %> ","<%= Cdata['PHONE'] %> ",<%= pid %>)' value='<%= Cdata["EMAIL"]  %>' ></input></td>
      <td> <%= Cdata['COMPANY'] %> </td>
      <td> <%= Cdata['FIRST NAME'] +' ' +Cdata['LAST NAME'] %> </td>
      <td> <%= Cdata['LOCATION'] %></td>
      </tr>
      <%
    }
  }); %>