javascript - How to split an array into a multidimensional array? -
so have json feed returns list of job titles. split parsed data split nodes of 3. example, right appending ones html 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 output 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 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 should go doing this?
do know modulo operator? http://en.wikipedia.org/wiki/modulo_operation
var currentblock; jobs.each(function(i, d){ if(i % 3 == 0){ //make new block currentblock = ... $("#careers .slides").append(currentblock) } // add stuff current block currentblock.append(...) })
Comments
Post a Comment