JavaScript - get array element fulfilling a condition

后端 未结 9 885
感动是毒
感动是毒 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:08

    In most browsers (not IE <= 8) arrays have a filter method, which doesn't do quite what you want but does create you an array of elements of the original array that satisfy a certain condition:

    function isGreaterThanFive(x) {
         return x > 5;
    }
    
    [1, 10, 4, 6].filter(isGreaterThanFive); // Returns [10, 6]
    

    Mozilla Developer Network has a lot of good JavaScript resources.

提交回复
热议问题