array.select() in javascript

前端 未结 5 2139
时光取名叫无心
时光取名叫无心 2021-01-31 06:44

Does javascript has similar functionality as Ruby has?

array.select {|x| x > 3}

Something like:

array.select(function(x) { i         


        
5条回答
  •  我在风中等你
    2021-01-31 07:24

    There is Array.filter():

    var numbers = [1, 2, 3, 4, 5];
    var filtered = numbers.filter(function(x) { return x > 3; });
    
    // As a JavaScript 1.8 expression closure
    filtered = numbers.filter(function(x) x > 3);
    

    Note that Array.filter() is not standard ECMAScript, and it does not appear in ECMAScript specs older than ES5 (thanks Yi Jiang and jAndy). As such, it may not be supported by other ECMAScript dialects like JScript (on MSIE).

    Nov 2020 Update: Array.filter is now supported across all major browsers.

提交回复
热议问题