-5

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?

eye_mew
  • 7,741
  • 7
  • 28
  • 42
  • `so that is left as an exercise to the reader.` At minimum you need to specify your question :) – gurvinder372 Oct 03 '16 at 14:16
  • What's the question? – Henrik Andersson Oct 03 '16 at 14:17
  • `filter` iterates over a set and returns a subset. An object cannot be iterated. There cannot be a subset of an object, there can only be new, smaller objects. – David Hedlund Oct 03 '16 at 14:18
  • 1
    How about applying array methods over [Object.values()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values) of ES7 – Redu Oct 03 '16 at 14:18
  • @DavidHedlund hmm...Sure, an object can't currently be iterated over. The question is - why not? – eye_mew Oct 03 '16 at 14:29
  • Objects are not iterable by default, because they [conflate](http://stackoverflow.com/a/38777177/6445533) real data and meta- (or program) data/behavior. Metadata/behavior should be marked as non-enumerable, but I guess this isn't something you want to rely on. So probably we'll get `Reflect.values`/`Reflect.entries`, because iterating over objects is meta programming. –  Oct 04 '16 at 06:42

2 Answers2

5

Answering the actual question:

They just haven't been proposed, defined, checked to make sure they don't conflict with other things, etc., etc. You're free to propose something on the TC-39 github page. I think we'll definitely see more of these kinds of utilities as people step up to define the semantics rigorously, just as we've seen Object.values added for ES2017. (I think we'll mostly see them as additional methods on Object and Reflect, though, not additions to Object.prototype. Adding things to Object.prototype at this stage is really, really hard to keep "web safe.")


Side note:

Instead, I have to do:

const lessThan10 = Object.keys(millionsOfLines)
    .filter(key=> millionsOfLines[key] < 10)
    .map(key=> millionsOfLines[key]);

Hmmm, I assumed you wanted to filter object properties:

const millionsOfLines = {
    windowsVista: 50,
    firefox: 9,
    google: 2000,
    quake3Engine: 0.3,
    mySQL: 12
};

const lessThan10 = Object.keys(millionsOfLines).reduce((obj, k) => {
    let v = millionsOfLines[k];
    if (v < 10) {
        obj[k] = v;
    }
    return obj;
}, {});

console.log(lessThan10);

Just one pass required, not two.

Community
  • 1
  • 1
T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • Nice `reduce` there. Sorry, I realised my code was actually wrong and fixed it right before you posted this – eye_mew Oct 03 '16 at 14:22
  • @eye_mew: Ah, yes, that code does more what I expected. Re the `reduce`: I used to consider the above an *abusage*, since the "accumulator" value never changes and it's easy when writing the callback to forget to return it. Maybe I still should consider it an abusage, but I'm fairly sure I'd be in the tiny minority, so... :-) – T.J. Crowder Oct 03 '16 at 14:23
1

I can't tell you why objects don't have these functions. It's probably becasue they are objects not hashtables, and initially were not meant to be used like maps. In ES6, you can use Maps instead, but this type is not supported by all major browsers.

Also, if you are looking for these functions, have a look at Lodash a widely used library with higher order functions implemented. This library has functions that support map, reduce, filter, flatten and many others over arrays and objects.

jorgonor
  • 1,379
  • 8
  • 14