Inside of the else clause, the commented out line gives the desired result, but the one below it causes { '1': [ 1.3 ], '2': [ 2.1 ] } to become { '1': [ 1.3 ], '2': 2 }. I don't understand why this is happening.
const groupBy = (array, callback) => {
let obj = {}
for (let i = 0; i < array.length; i++) {
let currKey = callback(array[i])
let newVal = array[i]
let currVal = obj[currKey]
if (!currVal) {
obj[currKey] = [newVal]
} else {
// obj[currKey].push(newVal)
obj[currKey] = currVal.push(newVal)
}
}
return obj
}
// Uncomment these to check your work!
var decimals = [1.3, 2.1, 2.4]
var floored = function(num) { return Math.floor(num) }
groupBy(decimals, floored); // expect { 1: [1.3], 2: [2.1, 2.4] }