array.select() in javascript

前端 未结 5 2069
时光取名叫无心
时光取名叫无心 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:20

    yo can extend your JS with a select method like this

    Array.prototype.select = function(closure){
        for(var n = 0; n < this.length; n++) {
            if(closure(this[n])){
                return this[n];
            }
        }
    
        return null;
    };
    

    now you can use this:

    var x = [1,2,3,4];
    
    var a = x.select(function(v) {
        return v == 2;
    });
    
    console.log(a);
    

    or for objects in a array

    var x = [{id: 1, a: true},
        {id: 2, a: true},
        {id: 3, a: true},
        {id: 4, a: true}];
    
    var a = x.select(function(obj) {
        return obj.id = 2;
    });
    
    console.log(a);
    
    0 讨论(0)
  • 2021-01-31 07:21

    There's also Array.find() in ES6 which returns the first matching element it finds.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

    const myArray = [1, 2, 3]
    
    const myElement = myArray.find((element) => element === 2)
    
    console.log(myElement)
    // => 2
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-31 07:25

    Underscore.js is a good library for these sorts of operations - it uses the builtin routines such as Array.filter if available, or uses its own if not.

    http://documentcloud.github.com/underscore/

    The docs will give an idea of use - the javascript lambda syntax is nowhere near as succinct as ruby or others (I always forget to add an explicit return statement for example) and scope is another easy way to get caught out, but you can do most things quite easily with the exception of constructs such as lazy list comprehensions.

    From the docs for .select() (.filter() is an alias for the same)

    Looks through each value in the list, returning an array of all the values that pass a truth test (iterator). Delegates to the native filter method, if it exists.

      var evens = _.select([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
      => [2, 4, 6]
    
    0 讨论(0)
  • 2021-01-31 07:26

    Array.filter is not implemented in many browsers,It is better to define this function if it does not exist.

    The source code for Array.prototype is posted in MDN

    if (!Array.prototype.filter)
    {
      Array.prototype.filter = function(fun /*, thisp */)
      {
        "use strict";
    
        if (this == null)
          throw new TypeError();
    
        var t = Object(this);
        var len = t.length >>> 0;
        if (typeof fun != "function")
          throw new TypeError();
    
        var res = [];
        var thisp = arguments[1];
        for (var i = 0; i < len; i++)
        {
          if (i in t)
          {
            var val = t[i]; // in case fun mutates this
            if (fun.call(thisp, val, i, t))
              res.push(val);
          }
        }
    
        return res;
      };
    }
    

    see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter for more details

    0 讨论(0)
提交回复
热议问题