简体   繁体   中英

Getting duplicates using underscore.js _.each function?

I'm doing something like this to get the JSON in an underscore template

 <% _.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++; 
      }
    })%>
  <% }); %>

The result gives me the json as a table with 2 rows having the same values.. I get something like this

 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

What am I doing wrong here.. I want to get rid of the duplicates

the structure of my 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": ""}]

Your inner-most _.each seems to be unnecessary:

  <% _.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>
      <%
    }
  }); %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM