0

I need help figuring out how to save the output from my array. I have tried appending the output to a csv, but this gives me an empty csv or a csv appended with the words [object] for each item in the array.

Here is a sample of the output in my array:

{
      date: '2014-01-25',
      firstBoxerRating: [Array],
      firstBoxerWeight: 235.5,
      judges: [Array],
      links: [Object],
      location: 'Golden Nugget Casino, Atlantic City',
      metadata: '<a href="/en/judge/401833">Joseph Pasquale</a> 75-77 | <a href="/en/judge/401066">Tony Perez</a> 78-74 | <a href="/en/judge/401734">Barbara Perez</a> 78-74\n' +
        '<br>',
      numberOfRounds: [Array],
      outcome: 'win',
      rating: 20,
      referee: [Object],
      result: [Array],
      secondBoxer: [Object],
      secondBoxerLast6: [Array],
      secondBoxerRating: [Array],
      secondBoxerRecord: [Object],
      secondBoxerWeight: 236.5,
      titles: []
    },
    {
      date: '2014-05-16',
      firstBoxerRating: [Array],
      firstBoxerWeight: 240.75,
      judges: [Array],
      links: [Object],
      location: '2300 Arena, Philadelphia',
      metadata: '<a href="/en/judge/402009">Pierre Benoist</a> 79-73 | <a href="/en/judge/401245">Lynne Carter</a> 78-74 | <a href="/en/judge/677642">Eric Dali</a> 79-73\n' +
        '<br>',
      numberOfRounds: [Array],
      outcome: 'win',
      rating: 20,
      referee: [Object],
      result: [Array],
      secondBoxer: [Object],
      secondBoxerLast6: [Array],
      secondBoxerRating: [Array],
      secondBoxerRecord: [Object],
      secondBoxerWeight: 238,
      titles: []
    },
    {
      date: '2014-08-02',
      firstBoxerRating: [Array],
      firstBoxerWeight: 236.5,
      judges: [Array],
      links: [Object],
      location: 'Revel Resort, Atlantic City',
      metadata: '  time: 1:48\n' +
        ' | <a href="/en/judge/401683">Lindsey Page</a>\n' +
        '<br>Williams down three times\n' +
        '<br>',
      numberOfRounds: [Array],
      outcome: 'win',
      rating: 20,
      referee: [Object],
      result: [Array],
      secondBoxer: [Object],
      secondBoxerLast6: [Array],
      secondBoxerRating: [Array],
      secondBoxerRecord: [Object],
      secondBoxerWeight: 233,
      titles: []
    },
    {
      date: '2014-09-19',
      firstBoxerRating: [Array],
      firstBoxerWeight: 237,
      judges: [Array],
      links: [Object],
      location: "Harrah's Philadelphia, Chester",
      metadata: '  time: 1:34\n' +
        ' | <span>referee:</span> <a href="/en/referee/401364">Benjy Esteves Jr</a><span> | </span><a href="/en/judge/401043">Bernard Bruni</a> | <a href="/en/judge/400983">Larry Hazzard Jr</a> | <a href="/en/judge/402781">Alan Rubenstein</a>\n' +
        '<br>',
      numberOfRounds: [Array],
      outcome: 'win',
      rating: 20,
      referee: [Object],
      result: [Array],
      secondBoxer: [Object],
      secondBoxerLast6: [Array],
      secondBoxerRating: [Array],
      secondBoxerRecord: [Object],
      secondBoxerWeight: 288,
      titles: []
    },
    {
      date: '2014-11-14',
      firstBoxerRating: [Array],
      firstBoxerWeight: 244,
      judges: [Array],
      links: [Object],
      location: "Harrah's Philadelphia, Chester",
      metadata: '  time: 2:28\n' +
        ' | <span>referee:</span> <a href="/en/referee/401364">Benjy Esteves Jr</a><span> | </span><a href="/en/judge/677642">Eric Dali</a> | <a href="/en/judge/400983">Larry Hazzard Jr</a> | <a href="/en/judge/671032">Mike Somma</a>\n' +
        '<br>',
      numberOfRounds: [Array],
      outcome: 'win',
      rating: 40,
      referee: [Object],
      result: [Array],
      secondBoxer: [Object],
      secondBoxerLast6: [Array],
      secondBoxerRating: [Array],
      secondBoxerRecord: [Object],
      secondBoxerWeight: 209,
      titles: []
    },

I am assuming that this output is a JSON object.

This is the code I used to produce this output:

async function writeData() {
    const csv = require('csv-parser')
    const results = [];
    fs.createReadStream('C:\\Users\\User\\Documents\\testingclean.csv')
        .pipe(csv())
        .on('data',(data)=> results.push(data))
        .on('end', async () => {
          const cookieJar = await getCookieJar();
          const promises = [];
          results.forEach((data) => {
            promises.push(boxrec.getPersonById(cookieJar,data.id));
          })
          const fighters = await Promise.all(promises); // Fighters is an array
          fighters.forEach((fighter) => {
              console.log(fighter.output);
          })
        });
    };
try {
    writeData();
} catch (error) {
    console.log("Error in writeData: " + error);
}

1 Answer 1

1

Something like this should work.

fighters.forEach((fighter) => {
    let data = '';

    for (const key in fighter.output) {
        if (Array.isArray(fighter.output[key])) {
            data += JSON.stringify(fighter.output[key]) + ',';
        } else if (typeof fighter.output[key] === 'object') {
            data += JSON.stringify(fighter.output[key]) + ',';
        } else {
            data += fighter.output[key] + ',';
        }
    }
    data = data.replace(/(^,)|(,$)/g, ""); // Remove trailing comma
    data += '\n'; // Add new line

    fs.appendFile('data.csv', data, (err) => {
        if (err) throw err;
    });
});

Didn't test the code. Also, the data.csv file might take some time to create depending on data size. The forEach look will finish even before nodejs is done with writing file. So, the program might exit but in the background writing might go on until its done. You have to maintain the async execution part. How to handle it is not in scope of this question.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.