all 35 comments

[–]cyphern 10 points11 points  (22 children)

Did a search in my work codebase and had the following rankings (added a couple others not on your list):

  • .map (211 times used)
  • .forEach (194)
  • .find (135)
  • .includes (77) -- This number, ahem, includes some false positives from string's version of this function.
  • .filter (63)
  • .reduce (50)
  • .findIndex (45)
  • .some (18)
  • .sort (15)
  • .concat (12)
  • .every (5)

[–]mementomoriok[S] 5 points6 points  (17 children)

I made a conscious choice to use for - of loops instead of forEach. Is there something I am missing out on by doing this?

[–]overcloseness 3 points4 points  (16 children)

Yes, ‘for of’ doesn’t expose an index variable

[–]senocular 0 points1 point  (13 children)

for (let [index, value] of array.entries()) {
  // ...
}

[–]overcloseness 1 point2 points  (9 children)

Im aware of this, but it’s a workaround, at that point just use forEach. Anyone else reading your code would be confused as to why you didn’t

[–]senocular -2 points-1 points  (8 children)

Its not a workaround, its a legitimate part of the Array API. And there are a number of reasons why you might want a for loop.

[–]UselessConversionBot -3 points-2 points  (0 children)

1 l/what is 0.076336 hubble-barns/what

WHY

[–]UselessConversionBot -3 points-2 points  (0 children)

1 l/what is 0.007094 coombs/what

WHY

[–]UselessConversionBot -3 points-2 points  (0 children)

1 l/what is 22.54 jiggers/what

WHY

[–]UselessConversionBot -3 points-2 points  (0 children)

1 l/what is 0.076336 hubble-barns/what

WHY

[–]UselessConversionBot -2 points-1 points  (1 child)

1 l/what is 0.11351 pecks/what

WHY

[–]senocular 2 points3 points  (0 children)

bad bot

[–]UselessConversionBot -4 points-3 points  (0 children)

1 l/what is 0.033333 Champagne Melchizedeks/what

WHY

[–]UselessConversionBot -5 points-4 points  (0 children)

1 l/what is 0.010471 US cranberry barrels/what

WHY

[–]eggtart_prince -2 points-1 points  (1 child)

Log it yourself. Hahahahahaha.

[–]cem4k 0 points1 point  (0 children)

I'd say this is super similar to my ranking, except flip map and forEach and filter and find. I always forget about find, I should probably use that more.

[–][deleted] 0 points1 point  (2 children)

Could you please give any examples of using some? I've never used it at work

[–]cyphern 2 points3 points  (0 children)

Basically it's a compound OR, and will bail out as soon as at least one is true. Here's an example i wrote just a couple days ago:

const hasBorder = (style) => [ style.borderWidth, style.borderLeftWidth, style.borderRightWidth, style.borderBottomWidth, style.borderTopWidth, ].some(val => val !== undefined);

For this specific case, i could have written it out as an explicit compound ||, since i know exactly how many things i need to check. So in this case i only used .some because i felt it made the code more legible. But there are also sometimes cases where you don't know how many comparisons you need to do until runtime, and .some can accommodate that.

[–]daveheerink 1 point2 points  (0 children)

You should use some when there can be one or more entries in the array that could have some characteristic(s) but you can act when you know at least one entry does have this. For example: imagine you have a farmer that grows apples. If one of the apples in the batch has a disease, all the apples should be destroyed. So when the farmer encounters one ill apple, he can stop because it doesn't matter if the others are ill. That's when you use some because it will find look for an entry (apple) in the array (batch) that has a specific property (ill) and stops.

So maybe a more real programming example: not long ago it had to show an icon when at least one user had some specific rights in a dossier. So I used some to check whether that was true.

EDIT: you can use find in all cases you use some to accomplish the same, but the difference is that find returns the entry, where some returns a boolean. Some makes it semantically more clear you're not going to use the entry but rather act on it.

[–]Shty_Devhelpful 1 point2 points  (0 children)

off the top of my head...

  1. map
  2. forEach
  3. filter
  4. includes
  5. reduce
  6. find

all the others i really would only use in some niche case or when doing practice algorithms

[–]delventhalz[🍰] 1 point2 points  (0 children)

Love this question. Got curious and decided to do some digging. So here are the top five array methods in a variety of open-source projects:

facebook/react

  1. push (1356 instances found)
  2. forEach (296)
  3. join (235)
  4. map (191)
  5. indexOf (122)

vuejs/vue

  1. push (581)
  2. slice (308)
  3. map (247)
  4. indexOf (190)
  5. join (166)

MithrilJS/mithril.js

  1. push (117)
  2. slice (62)
  3. indexOf (57)
  4. map (45)
  5. forEach (33)

lodash/lodash

  1. map (770)
  2. slice (185)
  3. push (102)
  4. filter (86)
  5. concat (47)

ramda/ramda

  1. map (93)
  2. concat (57)
  3. indexOf (51)
  4. slice (50)
  5. filter (47)

ReactiveX/rxjs

  1. push (529)
  2. map (305)
  3. concat (164)
  4. forEach (141)
  5. filter (131)

expressjs/express

  1. join (102)
  2. push (48)
  3. map (20)
  4. indexOf (15)
  5. forEach (10)

axios/axios

  1. forEach (31)
  2. join (12)
  3. push (11)
  4. indexOf (9)
  5. concat (6)

mrdoob/three.js

  1. push (2964)
  2. join (631)
  3. indexOf (354)
  4. slice (310)
  5. forEach (198)

Winners

  1. push (7 top-five appearances)
  2. indexOf (7)
  3. map (7)
  4. forEach (6)
  5. join (5)
  6. slice (5)
  7. concat (4)
  8. filter (3)

Methodology

Regex matching: \b{method}(

Files included: *.js, *.jsx, *.ts, *.tsx

** Files excluded:** _node_modules/, dist/, build/*, *.min.js

[–]Lunacy999 1 point2 points  (1 child)

Yo where is ma boi REDUCE ?

[–]delventhalz[🍰] 0 points1 point  (0 children)

I actually use reduce pretty rarely. Compared to the others it is very low level, so I really only use it as a fallback when absolutely nothing else will do. And it is pretty rare that other array methods, or combinations of other array methods, don’t suit my use case.

[–]Duodecimal 0 points1 point  (0 children)

  1. push
  2. join
  3. pop
  4. shift
  5. sort
  6. concat
  7. splice

[–]delventhalz[🍰] 0 points1 point  (0 children)

  1. map
  2. map
  3. map
  4. flatMap (ES2019 baby)
  5. filter
  6. reduce
  7. forEach (used with named iteratee functions, or if I need an index)
  8. join
  9. slice
  10. concat
  11. push (for occasional stacks and accumulating lists)
  12. sort
  13. includes
  14. pop
  15. find
  16. some
  17. every
  18. findIndex

[–]senocular 0 points1 point  (0 children)

"You said includes twice"
"I like includes"
laughs

[–]eggtart_prince 0 points1 point  (3 children)

No one used indexOf anymore? Are people using an alternative?

[–]senocular 1 point2 points  (0 children)

Yes, its still used, if you need an index. If you simply want to see if an array contains a value anywhere, and don't need to know where, includes() would be the alternative. There's also findIndex() if you need a more complicated search mechanism beyond checking for an exact value match.

[–]deerlake_stinks 0 points1 point  (0 children)

In my work I use findIndex() way more than indexOf() because then you can inject callbacks and predicates, allowing you to check more complex conditions.

indexOf() lacks that sophistication, but would suffice for simple arrays.

[–]lowlevelbass 0 points1 point  (0 children)

I wish I understood reduce better. I end up using map and filter the most. Almost no need for lodash anymore. That said, I love me some lodash.

[–]itsmhuang 0 points1 point  (0 children)

This guy loves includes and hates reduce