Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON array - delete first element

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?

like image 413
Jerry S. Avatar asked Mar 24 '26 10:03

Jerry S.


2 Answers

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>');
}
like image 183
Blazemonger Avatar answered Mar 26 '26 23:03

Blazemonger


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>');
  }
});
like image 44
Clive Avatar answered Mar 27 '26 00:03

Clive



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!