JavaScript - get array element fulfilling a condition

后端 未结 9 910
感动是毒
感动是毒 2020-12-08 06:18

I\'m learning JavaScript using W3C and I didn\'t find an answer to this question.

I\'m trying to make some manipulations on array elements which fulfill some conditi

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 07:06

    You can use Array.prototype.find, wich does exactly what you want, returns the first element fullfilling the condition. Example:

    > ([4, {a:7}, 7, {a:5, k:'r'}, 8]).find(o => o.a == 5)
    
    {a:5, k:'r'}
    

提交回复
热议问题