JSON array slicing to get the first five objects

I have a JSON file in which I read objects and display them in a div. But I just need to show only five objects, not all.

Below is the code I'm using

$.each(recentActdata.slice(0,5), function(i, data) { var ul_data = "<li><h3>"+ renderActionLink(data)+ "</h3></li>"; $("#recentActivities").append(ul_data); }); 

But this fragment does not work. The format of my JSON is

 [ { "displayValue":"Updated Guidelines", "link":"#", "timestamp":"29/06/2013 01:32" }, { "displayValue":"Logging", "link":"#", "timestamp":"28/06/2013 16:19" }, { "displayValue":"Subscribe", "link":"#", "timestamp":"21/06/2013 14:30" }, { "displayValue":"Artifactory Vs Nexus", "link":"#", "timestamp":"21/06/2013 13:39" }, { "displayValue":"CTT - Java 7", "link":"#", "timestamp":"20/06/2013 13:30" }, { "displayValue":"Added Artifactory Server", "link":"#", "timestamp":"19/06/2013 23:39" }, { "displayValue":"Estimation Template", "link":"#", "timestamp":"19/06/2013 23:39" }, { "displayValue":"GZIP compression in Tomcat", "link":"#", "timestamp":"14/06/2013 23:39" }, { "displayValue":"HBase Basics", "link":"#", "timestamp":"12/06/2013 23:39" } ] 

Please suggest how to achieve this. I can not give my name JSON.

+7
source share
1 answer

The code you posted works great!

Here is a demo: http://jsfiddle.net/enXcn/1/

Note that when you change the slice value, more or less elements are displayed that you must describe to them.

 HTML: <div id="recentActivities"></div> 

JS:

 var recentActdata = [ { "displayValue":"Updated Guidelines", "link":"#", "timestamp":"29/06/2013 01:32" }, { "displayValue":"Logging", "link":"#", "timestamp":"28/06/2013 16:19" }, { "displayValue":"Subscribe", "link":"#", "timestamp":"21/06/2013 14:30" }, { "displayValue":"Artifactory Vs Nexus", "link":"#", "timestamp":"21/06/2013 13:39" }, { "displayValue":"CTT - Java 7", "link":"#", "timestamp":"20/06/2013 13:30" }, { "displayValue":"Added Artifactory Server", "link":"#", "timestamp":"19/06/2013 23:39" }, { "displayValue":"Estimation Template", "link":"#", "timestamp":"19/06/2013 23:39" }, { "displayValue":"GZIP compression in Tomcat", "link":"#", "timestamp":"14/06/2013 23:39" }, { "displayValue":"HBase Basics", "link":"#", "timestamp":"12/06/2013 23:39" } ]; $.each(recentActdata.slice(0,5), function(i, data) { var ul_data = "<li><h3>"+ data.displayValue+ "</h3></li>"; $("#recentActivities").append(ul_data); }); 
+5
source

All Articles