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
760k186 gold badges1.4k silver badges1.5k bronze badges
1 Answer 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);
answered Jun 26, 2018 at 10:27
Leonid Pyrlia
1,7322 gold badges12 silver badges16 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js