jQuery 切片 JSON 数组以获取前五个对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17401700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Slicing a JSON array to get first five objects
提问by roger_that
I have a JSON file wherein am reading the objects and displaying them in a div. But i just need to show only five of the objects rather than all.
我有一个 JSON 文件,其中正在读取对象并将它们显示在 div 中。但我只需要显示五个对象而不是全部。
Below is the code which i am using
下面是我正在使用的代码
$.each(recentActdata.slice(0,5), function(i, data) {
var ul_data = "<li><h3>"+ renderActionLink(data)+ "</h3></li>";
$("#recentActivities").append(ul_data);
});
But this slice doesn't seems to work. The format of my JSON is
但是这个切片似乎不起作用。我的 JSON 格式是
[
{
"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't give my JSON a name.
请建议如何实现这一目标。我不能给我的 JSON 一个名字。
采纳答案by Stefan
The code you posted works fine!
您发布的代码工作正常!
Here is a demo: http://jsfiddle.net/enXcn/1/
这是一个演示:http: //jsfiddle.net/enXcn/1/
Note how, when you are changeing the value of slice, more or less elements are displayed, which is what you discribed it should do.
请注意,当您更改 slice 的值时,会显示更多或更少的元素,这是您描述它应该做的。
HTML: <div id="recentActivities"></div>
JS:
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);
});