Maureen Moore
Maureen Moore

Reputation: 1127

How do I convert my objects to an array in node.js?

When I execute the code below in node, the command prompt complains that it needs an array of objects in order to use objects-to-csv.

const ObjectsToCsv = require("objects-to-csv");

async function scrapeDescription(url, page) {
//more code ...
return {url, cats, main_img, name, descript, price};
}

async function saveDataToCsv(data) {
  const csv = new ObjectsToCsv(data);
  await csv.toDisk("spreadsheets/output.csv");
}

browser = await puppeteer.launch({ headless: false}); 
const descriptionPage = await browser.newPage();
for (var i=0; i< 2; i++){
 result = await scrapeDescription(scrapeResults[i], descriptionPage);
 console.log(result);
}
await saveDataToCsv(result);

When I don't use the saveDataToCsv(data) function, I get the following results:

{
  url: 'https://www.example.com/product_info.php?products_id=479684',
  cats: 'JEWELRY < ANKLET < FASHION < ',
  main_img: 'images/20200312/AK001501.jpg',
  name: 'Faceted Bead Pearl Link Anklet',
  descript: ' Style No : 479684 Color : Multi Theme : Pearl  Size : 0.2"H, 9" + 3" L  One Side Only Lead and Nickel Compliant Faceted Bead Pearl Link Anklet',
  price: '$2.25 / pc'
}
{
  url: 'https://www.example.com/product_info.php?products_id=479682',
  cats: 'JEWELRY < ANKLET < FASHION < ',
  main_img: 'images/20200312/AK0001.jpg',
  name: 'Freshwater Pearl Disc Beaded Anklet',
  descript: ' Style No : 479682 Color : Neutral Theme : Pearl  Size : 0.25"H, 9" + 3" L  One Side Only Lead and Nickel Compliant Freshwater Pearl Disc Beaded Anklet',
  price: '$3.75 / pc$40.50 / dz'
}

So what I get is a couple of objects and I want to convert them to an array so that I can use objects-to-csv.

Upvotes: 0

Views: 31

Answers (2)

Maureen Moore
Maureen Moore

Reputation: 1127

This question was solved at a different forum. The trick was to use the spread operator inside of the for loop. resultsArray = [...resultsArray,result];

Upvotes: 0

Jean-Claude
Jean-Claude

Reputation: 23

I would change

for (var i=0; i< 2; i++){
    result = await scrapeDescription(scrapeResults[i], descriptionPage);
    console.log(result);
}
await saveDataToCsv(result);

to

let arr = [];
for (var i=0; i< 2; i++){
    result = await scrapeDescription(scrapeResults[i], descriptionPage);
    arr.push(result);
    console.log(result);
}
await saveDataToCsv(arr);

That way, you'll get all of your scraped objects in an array. Let me know if that does it.

Upvotes: 1

Related Questions