Equivalent of Underscore _.pluck in pure JavaScript

前端 未结 6 1284
旧巷少年郎
旧巷少年郎 2020-12-14 07:05

I\'m trying to recreate the Underscore pluck function using pure JS. However, I keep getting an array of undefineds being returned, instead of the actual values from the pr

相关标签:
6条回答
  • 2020-12-14 07:46
    var array = {
    name: ["Pedro", "João", "Francisco", "Diogo"],
    ID: [1,2,3,4]
    };
    console.log(pluck(array,'name'));
    
    function pluck(array, key) {
       return (array[key]);
    };
    

    JSFiddle

    You can use this function, click in JSFiddle to see a Example!! :)

    0 讨论(0)
  • 2020-12-14 07:48

    Here's a working solution

    function pluck(array,key){
      var a = [],
         i1 = -1, i2 = array.length,
          o ;
    
        while(++i1 < i2)
        {
            o = array[i1] ; // you have to use the bracket operator here
            if(o.hasOwnProperty(key)) a[a.length] = o[key] ;
        }
      return a ;
    }
    

    I didn't really change all that much. The reason why your code failed is because you used the dot operator .key to access the named property of the array elements instead of the bracket operator [key]. When using a reference as a key you need to use the bracket operator.

    0 讨论(0)
  • 2020-12-14 07:51

    You can do it with the native JavaScript .map():

    Array.prototype.pluck = function(key) {
      return this.map(function(object) { return object[key]; });
    };
    

    edit — modifying built-in prototype objects should be done with care; a better way to add the function (should you be OK with the idea of doing so in general) would be with Object.defineProperty so that it can be made non-enumerable:

    Object.defineProperty(Array.prototype, "pluck", {
        value: function(key) {
            return this.map(function(object) { return object[key]; });
        }
    });
    
    0 讨论(0)
  • 2020-12-14 07:52

    In ES5:

    function pluck(array, key) {
      return array.map(function(obj) {
        return obj[key];
      });
    }
    

    In ES6:

    function pluck(array, key) {
      return array.map(o => o[key]);
    }
    
    0 讨论(0)
  • 2020-12-14 07:56

    You are so close. You need to change:

    newArr.push(arr[i].key);
    

    to:

    newArr.push(arr[i][key]);
    

    Consider this:

    var obj = { myKey: 'my Value', theKey: 'another value' };
    var theKey = 'myKey';
    
    alert(obj.theKey); // another value
    alert(obj[theKey]); // my Value
    // You can also send in strings here:
    alert(obj['theKey']); // another value
    

    Hope you get my point.

    0 讨论(0)
  • 2020-12-14 08:03

    How about a reduce:

    $.pluck = function(arr, key) { 
        return arr.reduce(function(p, v) { 
          return p.concat(v[key]); 
        }, []); 
    }
    
    var people = [
      { name: 'James', age: 26 }, 
      { name: 'Fred', age: 56 }
    ];
    
    $.pluck(people, 'age');
    => [26, 56]
    
    $.pluck(people, 'name');
    => ['James', 'Fred']
    
    0 讨论(0)
提交回复
热议问题