davsp davsp - 3 months ago 8
Javascript Question

Array.push not pushing properly

EDIT: I've added the res.send(json), wasn't included the snippet.

I'm trying to extract/scrape a website and combine their output into json data. However when I run the endpoint, the response combines every result into its key, instead of adding a record per iteration. To expound, I get:

{
item: "item1item2item3item4item5item6",
title: "title1title2title3title4title5title5",
price: "price1price2price3price4price5price6"
}


This is my target output format though..:

{ item: "item1",
title: "title1",
price: "price1",
itemlink: "itemlink1" },
{ item: "item2",
title: "title2",
price: "price2",
itemlink: "itemlink2" },
{ item: "item3",
title: "title3",
price: "price3",
itemlink: "itemlink3" }, etc...


Here's the below snippet:

request(url, function(error, response, html){
if(!error){
var $ = cheerio.load(html);

var json = [];
/* Pulls out all the titles
$('.item-name').each(function() {
var title = $(this).text();
json2.push({title: title});
})
*/

function getID(str) {
return str.split('viewitem.php?iid=')[1];
}

$('.catlist').each(function(key, index) {
var title = $('.item-name').text();
var price = $('.catprice').text();
var id = getID($('h2').children().attr('href'));
var itemlink = $('h2').children().attr('href');

json.push({
id: id,
title: title,
price: price,
itemlink: itemlink
});
})

}
res.send(json)

})


I'm out of my wits, already spent hours on this. Any idea why they are not iterating properly for me? Thanks in advance!

Answer
 $('.catlist').each(function(key, index) {
    var title = $(this).find('.item-name').text();
    var price = $(this).find('.catprice').text();
    var id = getID($(this).find('h2').children().attr('href'));
    var itemlink = $(this).find('h2').children().attr('href');

    var temp = 
    {   
        id: id, 
        title: title, 
        price: price, 
        itemlink: itemlink 
    };
    json.push(temp);
})

You need to find children for each .catlist and push them to array one by one

Comments