Let's say I have this object:
const millionsOfLines = {
windowsVista: 50,
firefox: 9,
google: 2000,
quake3Engine: 0.3,
mySQL: 12
};
In the same way I'd do for an array, if only I could do:
const lessThan10 = millionsOfLines.filter((value, key)=> value < 10);
Instead, I have to do:
const lessThan10 = {};
Object.keys(millionsOfLines).forEach(key=>{
if (millionsOfLines[key] < 10) {
lessThan10[key] = value;
}
});
What a hassle. The other functions should surely also work in exactly the same way as the array versions:
const thousandsOfLines = millionsOfLines.map((value, key)=> value * 1000);
const totalLines = millionsOfLines.reduce((total, currValue, currKey)=> total + currValue);
I planned on comparing that to what I'd actually have to type, but I cannot bear the pain, so that is left as an exercise to the reader.
So - why don't javascript objects have map/reduce/filter?