0

I need to get all element except first with Cheerio. So I select all and then try to delete first but after when I try to loop elements I get error that first element undefined...

var categories = $('.subtitle1');

console.log(categories.length);

delete categories[0];
delete categories['0'];

console.log(categories.length);

categories.each(function(i, element) {
    console.log(element.children);
});

Result:

15
15

TypeError: Cannot read property 'children' of undefined....

If I comment delete... everything works fine except that I have first element.

user1564141
  • 5,911
  • 5
  • 21
  • 18

1 Answers1

4

Maybe this could solve your problem:

var $element = $(<htmlElement>);

$element = $element.slice(1); // Return all except first.
// $element = $element.slice(1, $element.length);

Documentation: https://github.com/cheeriojs/cheerio#slice-start-end-

So in your case this should be work:

var categories = $('.subtitle1').slice(1);
categories.each(function(i, element) {
    console.log(element.children);
});
Bruno J. S. Lesieur
  • 3,612
  • 2
  • 21
  • 25