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
760k186 gold badges1.4k silver badges1.5k bronze badges
asked Jun 26, 2018 at 8:02

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);
answered Jun 26, 2018 at 10:27
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.