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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sean Paul
  • 19
  • 2

1 Answers1

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);
Leonid Pyrlia
  • 1,594
  • 2
  • 11
  • 14