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

create an empty array and add each object to it - an array of object appears

is it result your object?
add it to an empty array

you can use the es6 spread operator to do this quite easily

let arr = [];

for (var i=0; i< 2; i++){
 result = await scrapeDescription(scrapeResults[i], descriptionPage);
 arr = [...arr,result];
}
1 Like

Thank you, silicon.child, that worked like a charm.