I've got multidimentional array and I need to count chars vertically. No problem to count in row, but I can't iterate it like vertically. Tip please.
const arrayData = [
['a', 'b', 'c'],
['a', 'f', 'g'],
['b']
];
My code looks like this:
const countChars = (input, direction) => {
if (direction === 'row') {
return input.reduce((acc, curr) => {
acc[curr] = acc[curr] ? ++acc[curr] : 1;
return acc;
}, {});
}
if (direction === 'column') {
for (let row = 0; row < input.length; row++) {
for (let column = 0; column < input[row].length; column++) {
console.log(input[column][row]);
}
console.log('---');
}
}
}
But for columns I'm getting this as result:
a
a
b
---
b
f
undefined
---
c
So I'm losing there a char because of undefined.
The result should be like for columns:
{ 'a': 2, 'b': 1 }
{ 'b': 1, 'f': 1 }
{ 'c': 1, 'g': 1 }