I will like to not bring back or delete the first record in the array and append it to my page. I'm getting data from a JSON file below:
$(data.content).each(function(index, content){
$("#htmlID").append('<span>' + content.myinfo + ');
});
{
"content":
[
{"myinfo":"Bill Clinton 095 years old. "},
{"myinfo":"Bill Clinton 195 years old. "},
{"myinfo":"Bill Clinton 295 years old. "},
{"myinfo":"Bill Clinton 295 years old. "}
]
}
When i append my data to the page everything comes out perfect, but i will like to skip the first element (index)... i basically do not want to bring back Bill Clinton 095 years old for a particular output.
How do i do that? ( grep?, remove, delete? ).
What's the simplest approach?
You probably don't want to modify the array or JSON, so just test which element you're on when you perform the loop:
$(data.content).each(function(index, content){
if (index>0) { // skips first element
$("#htmlID").append('<span>' + content.myinfo + '</span>');
}
});
Or better yet, just use an old-fashioned for loop and save yourself some overhead:
for (var i=1; i<data.content.length; i++) {
$("#htmlID").append('<span>' + data.content[i].myinfo + '</span>');
}
If you just want to skip the first item this will do it:
$(data.content).each(function(index, content){
if (index > 0) {
$("#htmlID").append('<span>' + content.myinfo + '</span>');
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With