How do I convert my objects to an array?

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.

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

Glad you found an answer. Thanks for letting us know how you resolved it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.