Return object from array map inside a for loop JS

doctorhm007

Regular Member
Joined
Jul 12, 2016
Messages
297
Reaction score
100
I'm trying to return an object from an array map inside a for loop and push it to productsData array but I'm getting [ [Object], [Object] ]. The array in question is 'lol'. Here's the code below:

JavaScript:
for (let i = 0; i < productsUrls.length; i++) {
        let ficheTechnique = [];
        let request = await axios.get(productsUrls[i]);
        const $ = cheerio.load(request.data);
        let productUrl = productsUrls[i];
        let title = $('div.col10').find('h1.-fs20.-pts.-pbxs').text();
        let price = $('div.-phs').find('div span.-b.-ltr.-tal.-fs24').text();
        let description = $('div.card.aim.-mtm').find('div.markup.-mhm.-pvl.-oxa.-sc').text();
        let descriptionImg = $('div.card.aim.-mtm').find('div.markup.-mhm.-pvl.-oxa.-sc center img').attr('data-src');
        let lol = $('.row.-pas article').map((i, el) => {
            let h2 = $(el).find('h2.hdr.-upp.-fs14.-m.-pam').text();
            let characteristiques = $(el).find('.markup.-pam').text() || $(el).find('.-pvs.-mvxs.-phm.-lsn').text();
            return {
                h2,
                characteristiques
            };
        }).get();
        console.log('ficheTechnique', lol);
        productsData.push({
            productUrl,
            title,
            // price,
            // description,
            // descriptionImg,
            // ficheTechnique,
            lol
        })
    }

js object problem.PNG
 
Very hard to say without knowing what $(el).find('h2.hdr.-upp.-fs14.-m.-pam').text() as well as $(el).find('.markup.-pam').text() and $(el).find('.-pvs.-mvxs.-phm.-lsn').text() returns. The code itself looks ok, but that map() looks a bit weird to me. Have not used jquery/cheerio for a while, but I think I used to use .each(), and not .map(). For example, try the following instead (removed unused stuffs, feel free to add back. Also changed the way lol is taken)..


JavaScript:
const productsData = [];
for (let i = 0; i < productsUrls.length; i++) {
    let request = await axios.get(productsUrls[i]);
    const $ = cheerio.load(request.data);
    let productUrl = productsUrls[i];
    let title = $('div.col10').find('h1.-fs20.-pts.-pbxs').text();
    let lol = [];
    $('.row.-pas article').each((i, el) => {
        let h2 = $(el).find('h2.hdr.-upp.-fs14.-m.-pam').text();
        let characteristiques = $(el).find('.markup.-pam').text() || $(el).find('.-pvs.-mvxs.-phm.-lsn').text();
        lol.push({
            h2,
            characteristiques
        });
    });
    console.log('ficheTechnique', lol);
    productsData.push({
        productUrl,
        title,
        lol
    });
}
 
Last edited:
Very hard to say without knowing what $(el).find('h2.hdr.-upp.-fs14.-m.-pam').text() as well as $(el).find('.markup.-pam').text() and $(el).find('.-pvs.-mvxs.-phm.-lsn').text() returns. The code itself looks ok, but that map() looks a bit weird to me. Have not used jquery/cheerio for a while, but I think I used to use .each(), and not .map(). For example, try the following instead (removed unused stuffs, feel free to add back. Also changed the way lol is taken)..


JavaScript:
const productsData = [];
for (let i = 0; i < productsUrls.length; i++) {
    let request = await axios.get(productsUrls[i]);
    const $ = cheerio.load(request.data);
    let productUrl = productsUrls[i];
    let title = $('div.col10').find('h1.-fs20.-pts.-pbxs').text();
    let lol = [];
    $('.row.-pas article').each((i, el) => {
        let h2 = $(el).find('h2.hdr.-upp.-fs14.-m.-pam').text();
        let characteristiques = $(el).find('.markup.-pam').text() || $(el).find('.-pvs.-mvxs.-phm.-lsn').text();
        lol.push({
            h2,
            characteristiques
        });
    });
    console.log('ficheTechnique', lol);
    productsData.push({
        productUrl,
        title,
        lol
    });
}
Returns the same results. "h2" and "characteristiques" are just strings. This line " console.log('ficheTechnique', lol);" works fine but when pushed to the array it just returns an array of undefined objects.
 
"lol" is an array of objects as shown what are you expecting to see?
js object problem2.PNG

This is the desired output, but when pushed to "productsData" array, it returns the first image.
 
Last edited:
What if instead of

JavaScript:
lol.push({        
h2,        
characteristiques    
 });

you do
JavaScript:
lol.push({        
h2: h2,        
characteristiques : characteristiques    
 });


?

The former way of structuring objects is still a very recent phenomenon. Might not be supported by your node.
 
What if instead of

JavaScript:
lol.push({       
h2,       
characteristiques   
 });

you do
JavaScript:
lol.push({       
h2: h2,       
characteristiques : characteristiques   
 });


?

The former way of structuring objects is still a very recent phenomenon. Might not be supported by your node.
Same thing, might be something to do with scopes?
 
Same thing, might be something to do with scopes?
I doubt it. The scoping looks ok from what I can see. I believe somehow, it's a timing issue in the sense that the elements are really somehow returning [object object] instead of string.
 
I doubt it. The scoping looks ok from what I can see. I believe somehow, it's a timing issue in the sense that the elements are really somehow returning [object object] instead of string.
Will look more into it as I need data organized in that way. Thanks for your help! Will update once found the solution.
 
Yo dude, cool that you've solved it but here's a tip. Use a debugger. I see people spending hours on tiny problems where a debugger would've helped you get to the bottom of the problem in a few minutes. It's easy to set up if you're using an IDE. If you aren't using an IDE then wtf, use one ;)
 
Yo dude, cool that you've solved it but here's a tip. Use a debugger. I see people spending hours on tiny problems where a debugger would've helped you get to the bottom of the problem in a few minutes. It's easy to set up if you're using an IDE. If you aren't using an IDE then wtf, use one ;)
Thanks for the advice. Started using it and it really is a great help!
 
Back
Top