Crawlling html and save json

  javascript, jquery, json

im crawlling a html page, and is working fine but im having a issue, when im looping a rows from a table, and saving it in a json file, when finishing the loop instead of having 7 records i get the one 1 record and the last one from the loop.

My code:

 finalResult[date] = {};

                    $(this).find('tbody tr').each(function (i, el) {
                        var $tds = $(this).find('td')
                        var counter = i +1;

                        $tds.each(function(index, element){

                            var $th = $(this).closest('table').find('th').eq($(this).index());

                            if($th.text() != ""){
                                var temp = { [counter] : $(this).text()};
                                finalResult[date][$th.text()] = { ...temp };

                            }

                        });

                    });

Im getting this result:
{
‘Satuday, 31 Jan 2021’: { PTM: { ‘7’: ‘402 – 1’ }, PT: { ‘7’: ‘905 – 2’ } }
}

When it should be like:

{
  'Satuday, 31 Jan  2021': { PTM: { '1': '442 - 1','2': '442 - 1',.... ,'6': '402 - 1','7': '402 - 1' }, PT: { '1': '442 - 1','2': '442 - 1',.... ,'6': '402 - 1','7': '905 - 2' } }
}

Source: Ask Javascript Questions

One Reply to “Crawlling html and save json”

LEAVE A COMMENT