Merge three array into a string

Chris :

I have merged 2 arrays in to a customized string. Now I am trying to merge 3 array but that doesn't came as expected.

2 Arrays

         A = ['Name' , 'Age'];
         B = ['abc',10];

         [A, B].reduce((a, b) => a.map((v, i) =>
                  v + " is " + b[i]  + "\n"
                ))

O/P

["Name is abc","Age is 10"]

3 Arrays

         A = ['Name' , 'Age'];
         B = ['is' , 'is'];
         C = ['abc',10];

         [A, B].reduce((a, b,c) => a.map((v, i) =>
                  v + b[i] + " " +c[i]  + "\n"
                ))

I am getting too many undefined value with this. Where I am going wrong?

Maheer Ali :

You should use map() on one of the array and then get the values of corresponding index from other two arrays. Use join() to get a main string

let A = ['Name', 'Age'];
let B = ['is', 'is'];
let C = ['abc', 10];

const res = A.map((x, i) => x + ' ' + B[i] + ' ' + C[i]).join('\n');
console.log(res)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=31893&siteId=1