Ad

Get Text Of Multiple Elements As Separate Values

I'm trying to collect info from WEB, but if there is 2 elements in one page, info stored in same obj line. How to store info in different line? code:

if (!error && response.statusCode == 200) {
  var $ = cheerio.load(html);
  $('#ctl00_MainContent_ipcAvaDay_upnlResultOutbound').each((e, el) => {
    var arrival_airport = $(el).find('.rowinfo2 td:nth-child(2)').text();
    var alio = [arrival_airport];

and the console.log im getting is

[RigaRiga] 

instead of

[Riga]
[Riga]

maybe some ideas ?

Ad

Answer

When text() is called on a collection of elements jQuery will concatenate all the values in to a single string.

If you want to create an array of all the separate values, use map():

var arrival_airport = $(el).find('.rowinfo2 td:nth-child(2)').map(function() {
  return $(this).text();
}).get();
console.log(arrival_airport);
Ad
source: stackoverflow.com
Ad