I'm trying to make an array from a few arrays.
For example :
array1= ['Joe','James','Carl','Angel','Jimmy',];
array2= ['22','11','29','43','56',];
array3= ['red','blue','orange','brown','yellow',];
I need to create a new array pushing by index. What I'm trying to do is something like this :
newArray=['Joe22red','james11blue','Carl29orange','Angel43brown','Jimmy56yellow']
How can I do that ?
array1= ['Joe','James','Carl','Angel','Jimmy'];
array2= ['22','11','29','43','56'];
array3= ['red','blue','orange','brown','yellow'];
newArray = []
for (let i =0; i<Math.max(array1.length, array2.length, array3.length); i++) {
newArray.push(`${array1[i]}${array2[i]}${array3[i]}`)
}
console.log(newArray);
You can use map function to find out the result
const array1= ['Joe','James','Carl','Angel','Jimmy',];
const array2= ['22','11','29','43','56',];
const array3= ['red','blue','orange','brown','yellow',];
const result = array1.map(
(item, index) => `${item}${array2[index]}${array3[index]}`
)
console.log(result);
And if we don´t know how many arrays are? Here is solution
const array1= ['Joe','James','Carl','Angel','Jimmy',];
const array2= ['22','11','29','43','56',];
const array3= ['red','blue','orange','brown','yellow',];
const data = [array1, array2, array3];
const result = data[0].map((_, index) => data.map((arr) => arr[index]).join(''));
console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}