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?

4
  • 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. Commented Aug 26, 2013 at 16:12
  • Please show an attempt so we can understand what you're having difficulty with Commented Aug 26, 2013 at 16:17
  • 2
    The problem doesn't seem to have anything to do with JSON but with how to process JavaScript arrays. Commented Aug 26, 2013 at 16:17
  • possible duplicate of Partitioning in JavaScript Commented Aug 26, 2013 at 16:20

3 Answers 3

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(...)
})
Sign up to request clarification or add additional context in comments.

Comments

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/

Comments

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>'
      );  
    });
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.