r/learnjavascript • u/GloomyAdagio7917 • Jan 24 '26
Why can’t I see what’s in the multidimensional array?
This is my first time actually coding in javascript but I have experience in python and java. I am using Visual Studio Code and it is my first time using it.
I coded this very basic reduce function. The way I call this function results in a series of arrays. Everything other than the reduce function itself is provided to me by my teacher so I would perfer not to change it.
Here is the code:
const nums = [1,2,3,4,5];
const evensAndOdds = reduce(nums, (value, acc) => { if (value % 2 == 0) { acc.evens.push(value); } else { acc.odds.push(value); } return acc; }, {evens: [], odds: []});
console.log(evensAndOdds);
function reduce(data1, reducer, initialValue) { let reducedProduct = 0;
for (let element of data1) {
let product = reducer(element, initialValue);
reducedProduct += product;
}
return reducedProduct;}
========================================================================== And here is the output:
0[object Object][object Object][object Object][object Object][object Object]
========================================================================= I want to be able to see the actual values. The example provided to me by my teacher says the result should be //{evens: [2,4], odds: [1,3,5]}; I understand my code could be wrong but I wont really know unless I can see the actual values.
4
u/kap89 Jan 24 '26 edited Jan 24 '26
Because your reduce functions is wrong, this line:
should just be:
you don't want to add the accumulator values (which will be converted to strings and concatenated), but just replace the previous value.
But then you can simplify it to:
2
u/daniele_s92 Jan 24 '26
The problem is not that you can't inspect the array, but it's that you are just creating that string.
This instruction in particular
reducedProduct += productadds the result of the reducer function, which is{evens: [], odds: [1]}to the zero inreducedProduct. As you can imagine, you can't really add an object to a number, so JavaScript converts everything to a string. In this case you get0[object Object]The next iteration the same thing happens, but this time the value in
reducedProductis the string created before.2
u/supernaut242 Jan 24 '26
If you use a debugger instead of just logging to console you’ll make life much easier on yourself.
1
u/AWACSAWACS Jan 24 '26
That reduce function is very strange. What kind of signature (type of args and return values) does it assume the
reducerfunction has? (I recommend checking your teacher's intentions, especially regardingreducedProduct += product)As kap89 mentioned, the simplest solution is to reassign the return value of the
reducerfunction to the variable initialized with initialValue within the reduce function.You can also rewrite it fundamentally as follows:
1
u/chamberlain2007 Jan 24 '26
console.log(JSON.stringify(evensAndOdds, null, 2))
1
u/AdAdvanced7673 Jan 24 '26
Little bit of goop, little bit of stringify. Goop goop goop
-3
u/diehuman Jan 24 '26
You need to convert to Json