0

我正在做这样的事情来在下划线模板中获取 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": ""}]
4

1 回答 1

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>
      <%
    }
  }); %>
于 2013-02-13T08:36:04.770 回答