The Most POWERFUL JavaScript Method?

preview_player
Показать описание

For your reference, check this out:

If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!

#dcode #javascript
Рекомендации по теме
Комментарии
Автор

There is a simpler way to do this rather than passing null as the second argument to reduce and then checking for null to distinguish the first iteration.

If you don't include a second parameter to reduce then the first item in the array will be used for the accumulator, what you are calling here "currentHighestEarner".

Therefore it's always going to check against a person object and it's salary, so you don't need to check for null. :)

smoothbeak
Автор

Thanks for sharing Dom. Your videos always get me thinking "Hmm, interesting, but how else could I achieve the same, or better result" - and I ended up writing this;

const [theHighestEarner = null, theSecondHighestEarner = null] = people.sort((a, b) => b.salary - a.salary);

The advantage of using a sort method, is you can then use deconstruction to return not only the top salary, but the top 2 (or more), for example.

karlstenator
Автор

Hello sir wahich theme did you use vs code

techhub
Автор

I want more. Even our professor told us not to pay attention to reduce() since its rarely used. Can you give more practical exams of .reduce()?

czar_cz
Автор

Anymore plans for Rust, you did a neat series on it a few years back.

lardosian
Автор

most if not all array loop method in javascript can be done with just one forEach and an external variable / let

flowerofash
Автор

Always Always Always high quality and underrated JS Channel! Finally something more in-depth thank you Dom!

kosnowman
Автор

I also feel like learning the reduce method made me a better programmer. I feel like I can get it to do anything...eventually.

bmehder
Автор

I have learned something new, thank you 🙏🤓😄😌👏😍🤩😘🤗👍

maskman
Автор

The currentHighestEarner == null is just there to prevent that initial error right?

Obviously it still checks the second conditional but im a bit confused because in my mind it would return the person as null as soon as currentHighestEarner == null becomes true.

TsoiIzAlive
Автор

I think we should let the initial value by the people[0] rather than null

tranphat
Автор

thx for your work - aspecially for pronounce and gramaticly correct speech :) It is make my own soft-skills better on a common themes, linked with web development :)

ТимофейКривенко-вл
Автор

You may use 0 as initial value so you won't need to make that ugly condition.

podstrahuy
Автор

Finally, I understand the reduce() method. Thanks to you, Dom.

{2023-03-09}

Pareshbpatel