0

So I have a JSON feed that returns a list of job titles. I would like the split the parsed data so that they are split into nodes of 3. So for example, right now I am appending all the ones into HTML that looks like:

<div class="slide">
  <div class="jobs-list">
    <a href="#" class="job">Title 1</a>
    <a href="#" class="job">Title 2</a>
    <a href="#" class="job">Title 3</a>
    <a href="#" class="job">Title 4</a>
    <a href="#" class="job">Title 5</a>
   </div>
</div>

I would like the output to look like:

<div class="slide slide1">
  <div class="jobs-list">
    <a href="#" class="job">Title 1</a>
    <a href="#" class="job">Title 2</a>
    <a href="#" class="job">Title 3</a>
   </div>
</div>
<div class="slide slide2">
  <div class="jobs-list">
    <a href="#" class="job">Title 4</a>
    <a href="#" class="job">Title 5</a>
   </div>
</div>

Here is my current JS

$.get('sample-json/9.json', function (data) {
  var data = $.parseJSON(data);
  console.log(data);

  if (data.result.length === 0) {
    alert("No Data. Show Error Screen.");
  } else {
    count = 0;
    count++;
    $("#careers .slides").append('<div class="slide slide' + count + '"></div>');
    $('.slide' + count).append('<div class="jobs-list"></div>');

    $(data.result).each(function (i, d) {
      $('.slide' + count).find(".jobs-list").append(
        '<a class="job cf" href="#">'+ d.type + '</a>');
    });

  }
});

Any pointers on how I should go about doing this?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
raeq
  • 971
  • 4
  • 15
  • 36
  • 2
    Just iterate over it with a `if (index % 3 == 0)` which causes it to create a new group to start inserting into on every 3rd item. – Kevin B Aug 26 '13 at 16:12
  • Please show an attempt so we can understand what you're having difficulty with – Ruan Mendes Aug 26 '13 at 16:17
  • 2
    The problem doesn't seem to have anything to do with JSON but with how to process JavaScript arrays. – Felix Kling Aug 26 '13 at 16:17
  • possible duplicate of [Partitioning in JavaScript](http://stackoverflow.com/questions/11345296/partitioning-in-javascript) – Felix Kling Aug 26 '13 at 16:20

3 Answers3

2

Do you know the modulo operator? http://en.wikipedia.org/wiki/Modulo_operation

var currentBlock;
jobs.each(function(i, d){
  if(i % 3 == 0){
    //make a new block
    currentBlock = ...
    $("#careers .slides").append(currentBlock)
  }
  // add stuff to the current block
  currentBlock.append(...)
})
posit labs
  • 8,951
  • 4
  • 36
  • 66
0

If you process your JSON into this HTML structure (similar to what you already did):

<div class="slides">
    <a href="#" class="job">Title 1</a>
    <a href="#" class="job">Title 2</a>
    <a href="#" class="job">Title 3</a>
    <a href="#" class="job">Title 4</a>
    <a href="#" class="job">Title 5</a>
</div>

Then you can manipulate it afterwards into the structure you want, using this JS:

var count = 1;
while ($("div.slides > a.job").length) {
    $("div.slides").append("<div class='slide slide" + count + "'></div>");
    for (var i = 0; i < 3; i++) {
        $("div.slides > a.job:first").appendTo($("div.slide" + count));
    }
    count++;
}
$("div.slide").wrapInner("<div class='jobs-list'></div>");

fiddle: http://jsfiddle.net/Vcjs4/

andi
  • 6,442
  • 1
  • 18
  • 43
0

I was able to solve the problem using a modulus operator.

$.get('sample-json/9.json', function(data) {
  var data = $.parseJSON(data);

  if( data.result.length === 0 ) {
    alert("No Data. Show Error Screen.");
  } else {
    count = 0;
    $( data.result).each(function(i,d){
      if(i % 6 == 0){
        count++;
        $("#careers .slides").append('<div class="slide slide' + count + '"></div>');
        $('.slide' + count).append('<div class="jobs-list"></div>');
      }
      $(".slide" + count).find(".jobs-list").append( 
        '<a class="job cf" href="#" target="_blank">' + d.title + '</a>'
      );  
    });
  }
});
michaelok
  • 1,124
  • 1
  • 13
  • 20
raeq
  • 971
  • 4
  • 15
  • 36