So, I am trying to regroup the elements... well in a way that is hard to explain. Here is a sample of input and expected output...
zip(['fred', 'barney'], [30, 40], [true, false]);
should output...
ā [['fred', 30, true], ['barney', 40, false]]
I thought reduce would be appropriate since I am supposed to take multiple arrays and convert it into a single array that contains the same amount of arrays as the input array's length...
Here is what I am working on... It isn't functioning but I believe I am close to the right idea!
function zip(array) {
return array.reduce((acc, next) => {
// get length of next array to use in for-loop...
let numOfElem = next.length
// use a for loop to access different indexed arrays...
for (let i = 0; i < numOfElem; i++) {
// this is supposed to push the appropriate element in the next array to the accumulator array's corresponding index...
acc[i].push(next[i]);
}
return acc;
}, [])
}
const result = zip(['fred', 'barney'], [30, 40], [true, false]);
console.log(result);
I believe I am attempting to push incorrectly? The idea behind acc[i].push(next[i]) is that acc[i] would create the necessary amount of arrays based off of the length of the input arrays. The code is non-functional. I am just looking for a way to get it working, even if by a different method!
Thanks for taking the time to read this and for any feedback, tips or tricks!