Carl Edwards
Carl Edwards

Reputation: 14454

Mass assignment for elements in a for loop

I'm dealing with pulling in information from an API and want to take that data and inserting it into certain elements of different classes within a for-loop. Right now I'm doing this through a switch-statement wrapped inside an .each() method. It just seems that this way for one is repetitive and probably is more performance heavy than anything. Is there a way to accomplish this in a more concise and efficient way?

var tracks = [{number: "01", title: "Track 1", duration: "5:35"}, {number: "02", title: "Track 2", duration: "5:15"}, {number: "03", title: "Track 3", duration: "5:07"}, {number: "04", title: "Track 4", duration: "0:16"}, {number: "05", title: "Track 5", duration: "5:35"}];

for (var i = 0, trackNumber, trackTitle, trackDuration; i < tracks.length; i++) {
  trackNumber   = tracks[i]["number"];
  trackTitle    = tracks[i]["title"];
  trackDuration = tracks[i]["duration"];
  
  $("span").each(function() {
    switch($(this).attr("class")) {
      case "number":
        $(".number").eq(i).text(trackNumber);
      case "title":
        $(".title").eq(i).text(trackTitle);
      case "duration":
        $(".duration").eq(i).text(trackDuration);
      default:
        //
    }
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <thead>
    <tr>
      <td><b>No</b></td>
      <td><b>Title</b></td>
      <td><b>Duration</b></td>
    </tr>
  </thead>
  <tr>
    <td><span class="number"></span></td>
    <td><span class="title"></span></td>
    <td><span class="duration"></span></td>
  </tr>
  <tr>
    <td><span class="number"></span></td>
    <td><span class="title"></span></td>
    <td><span class="duration"></span></td>
  </tr>
  <tr>
    <td><span class="number"></span></td>
    <td><span class="title"></span></td>
    <td><span class="duration"></span></td>
  </tr>
  <tr>
    <td><span class="number"></span></td>
    <td><span class="title"></span></td>
    <td><span class="duration"></span></td>
  </tr>
  <tr>
    <td><span class="number"></span></td>
    <td><span class="title"></span></td>
    <td><span class="duration"></span></td>
  </tr>
</table>

Upvotes: 1

Views: 221

Answers (3)

Ram
Ram

Reputation: 144699

You could code:

$('tbody td span').text(function() {
    var i = this.parentNode.parentNode.rowIndex - 1;
    return tracks[i][this.className];
});

In the above snippet rowIndex of the tr elements is used for getting a specific array's element by index. Using bracket notation ([index]) and the className of the span element the corresponding property's value is set by the text method. Here is a demo.

As the table has one 1 row in the thead element the rowIndex of the first tr child of the tbody element is 1. That's why the returned value is subtracted by 1.

As a note, you should consider generating the tr elements according to the returned data. Using a templating library can be an option.

edit: You could also use vanilla JavaScript:

var spans = document.querySelectorAll('tbody td span');
Array.prototype.forEach.call(spans, function (el) {
    var i = el.parentNode.parentNode.rowIndex - 1;
    el.textContent = tracks[i][el.className];
});

Upvotes: 1

eladcon
eladcon

Reputation: 5825

How about:

var attr = $(this).attr("class");
$("." + attr).eq(i).text(tracks[i][attr]);

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82241

You can rather use:

 $("span.number").eq(i).text(trackNumber);
 $("span.title").eq(i).text(trackTitle);
 $("span.duration").eq(i).text(trackDuration);

Upvotes: 1

Related Questions