0

I was following the mentioned question and this one: JavaScript array to CSV

Trying to print 2 arrays in columns A and B which i can turn into a string separated by comas"," which ever is more easy to do but i just can´t get it to work with the examples available.

My arrays look like this:

var Test = ["John", "Ivar", "Peter", "Tony"];
var Addres = ["Canada", "Sweden", "England", "Chile"];

And in string format its the same only separated by comas

I thought it was going to be an easy task but it's more complicated than I expected

Hope anyone here can help me with this,

Thanks

1 Answer 1

1

You can accomplish it with one-liner reduce:

var Test = ["John", "Ivar", "Peter", "Tony"];
var Addres = ["Canada", "Sweden", "England", "Chile"];

var result = Test.reduce((str, name, i) => `${str}${name},${Address[i]}\n`, 'Test,Address\n');

console.log(result);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.