S
S
sashavol2017-04-04 00:15:38
JavaScript
sashavol, 2017-04-04 00:15:38

Correct work of each in cheerio?

Goodnight! Need help!
There is this code:

var array = [], s_string = '';
  $('.on-map article').each(function() { 
  s_string += $(this).find('b').first().text();
       //в конце весь результат пушу в массив
      array.push(s_string);
  })

I go through the pages with a grabber (or parser) using the cheerio module, on some pages there are from 1 to 8 blocks of which I go through, all the blocks are in the "article" tag, respectively, when it enters the page and there 1 block everything passes correctly and saves, but as soon as there are 2 or more blocks on the page for saving, things that are not clear to me begin.
Specifically, it turns out that if there are several blocks on the page, then it saves as follows:
1 страница ['значение 1']
2 страница ['значение 1'] // Тут 2 блока
2 страница ['значение 1'] // Тут 2 блока
2 страница ['значение 2'] // Тут 2 блока

Probably not clear explained :). In short, it goes through all these blocks as many times as blocks and each time returns incrementally, if there are 3 blocks on the page, then it will first return 1, then 1.2, then 1,2,3. My meager knowledge of jquery has been exhausted :) And it turns out that not unique data is stored in the array, but duplicated as many times as blocks

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question