0

I have a array map function which helps me reduce an array of objects down to a simple string.

const formatEmails: (arr: { "default" : string }[]) => string = (arr: { "default" : string }[]) => 
arr.map(e => e["default"]).reduce((e, i) => e + i + "; ", "");

this outputs me the actual email address with out the default. But what if my email looks like this and does not only have default as a key ?

 person_emails: [{ "default": '[email protected]' }, { "home": '[email protected]' }, { "work": '[email protected]' }]

How would i go about that and how can i generate a string of for example "default:[email protected]; home:[email protected]; work:[email protected]"

Also just to be clear my code calls the function formatEmails everytime we read a row on the data export like this

args.rowData["person_emails"] = formatEmails(args.rowData["person_emails"]);

1 Answer 1

1

Simply join the object entries

const person_emails = [{ "default": '[email protected]' }, { "home": '[email protected]' }, { "work": '[email protected]' }];
const res = person_emails.map(e => 
  Object.entries(e).map(en => en.join(':')).join('; ')
).join('; ');
console.log(res);

Above works also for multiple pairs in a single object

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

1 Comment

Yes that works as a standalone, but in my case what would i change the line const formatEmails: (arr: { "default" : string }[]) => string = (arr: { "default" : string }[]) => arr to as i no longer need to descibe the array ? Also i am getting an error on Object.entries(e) saying Properties 'entries' does not exist on 'ObjectConstructor'

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.