Getting the values for a specific key from all objects in an array

后端 未结 7 1995
醉酒成梦
醉酒成梦 2020-12-15 04:32

I\'m running an express.js app that has a few apis feeding data to dropdown boxes. The data returned is in the form:

  [
    {
        key: \'blah\',
                


        
7条回答
  •  庸人自扰
    2020-12-15 04:56

    You could map:

    permittedValues = array.map(function(value) {
      return value.key;
    });
    

    In ES6/ES2015 it's even prettier with arrow functions:

    permittedValues = array.map(value => value.key);
    

    It might be prettier, but it's probably not faster than a for() loop.

提交回复
热议问题