I want to take the first character of each item in an array and join those to create a new string. Then i want to do the same with the second characters, third characters, and finally 4th characters.
For example I have the following array:
let arr = ["abcd", "efgh, "ijkl", "mnop"]
would then turn into the string…
"aeim"
"bfjn"
"cgko"
"dhlp"
3
Answers
Assuming that each element in the array has the same length, you could iterate over all indexes and use
Array#map
andArray#join
to construct a string from all the characters at that index.(If you want to get the length of the shortest string when they have different lengths, you can use
Math.min(...arr.map(s => s.length))
.)You can achieve this by using a combination of the
Array#map
andArray#join
it back to string:Output:
Another method is to iterate over each position in the strings with simply two
for-loops
: