If you use arrows functions, then I don't see any reason why not to use find instead of forEach. It's simple and more elegant than the forEach loop. Besides, I would rather say this:
fruits[0] = 'apple'
fruits[1] = 'orange'
is not the ordinary way you add elements to the array.
Yes you are right for both suggestions,
Usually we use push as basic operation to add element.
I will remain forEach to show that loop is also used to get many elements by condition then i will add find method example too. Thanks
I would say that depends on what you need to do, it might be better to use either map or filter, which return a new array. It comes very handy in almost every situation. I used those both methods very often.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
If you use arrows functions, then I don't see any reason why not to use
find
instead offorEach
. It's simple and more elegant than theforEach
loop. Besides, I would rather say this:is not the ordinary way you add elements to the array.
Yes you are right for both suggestions,
Usually we use push as basic operation to add element.
I will remain forEach to show that loop is also used to get many elements by condition then i will add find method example too. Thanks
I would say that depends on what you need to do, it might be better to use either
map
orfilter
, which return a new array. It comes very handy in almost every situation. I used those both methods very often.