0

I am pulling some email addresses data from our db to print to a file in our Node project. That code looks like this:

  const sql = `${query}`;
  let emailRecords = await this.mariaDB.query(sql);
  let data = JSON.stringify(emailRecords);
  fs.writeFileSync(appRoot + "/public/email-lists/staff-emails.json", data); 

That gives me data that looks like this:

[ { email: 'email1@yahoo.com' },
  { email: 'email2@hotmail.com' },
  { email: 'samp2@yahoo.com' },
  { email: 'samp3@gmail.com' } ]

What I want to do is get rid of the key/value pairs, and end up with an array of just email addresses, like so:

[ 'email1@yahoo.com',
  'email2@hotmail.com',
  'samp2@yahoo.com',
  'samp3@gmail.com' ]

I'm not sure what's the best way to accomplish this, as 'filter()' is used to return a subset of the original array based on matching criteria. Here I want all of the original data, but trimmed like I describe above. Should I use regex here? Something else?

Rey
  • 1,393
  • 1
  • 15
  • 24

1 Answers1

1

You can use the map high order function to create a new array like this:

const emails = data.map(e => { return e.email })

the new variable emails is a new array with the values that you need.

Luis Louis
  • 644
  • 6
  • 21