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

后端 未结 7 1966
醉酒成梦
醉酒成梦 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:51

    You could extract the values via map, and form them into a regex to match values against.

    Example: http://repl.it/X0V

    var items=
    [
        {
            key: 'blah',
            value: 'Blah Blah'
        },
        {
            key: 'foo',
            value: 'Foos'
        },
        {
            key: 'bar',
            value: 'Bars'
        },
        {
            key: 'baz',
            value: 'Bazingo'
        }
    ];
    
    var toReg = items.map(function(obj){
        return obj.key; 
    }).join('|');
    var regex = new RegExp('^('+ toReg +')$');
    
    //To test the regex
    var itemsToTest = ['blah', 'Pies', 'foo', 'Bazingo'];
    itemsToTest.forEach(function(key){
       if(regex.test(key)){
           console.log(key);
       }
    });
    
    0 讨论(0)
  • 2020-12-15 04:55

    Using lodash,

    Since lodash 4.x the _.pluck function has been removed in support to map function.

    so you can achieve the desired task by:

    import _ from 'lodash'
    _.map(items, 'key');
    

    Ref: What happened to Lodash _.pluck?

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-15 04:56

    If you are using ES6 javascript version you can do something like the one below:

    const arrayData = [
        {
            key: 'blah',
            value: 'Blah Blah'
        },
        {
            key: 'foo',
            value: 'Foos'
        },
        {
            key: 'bar',
            value: 'Bars'
        },
        {
            key: 'baz',
            value: 'Bazingo'
        }
    ];
    const foodBar = arrayData.find(item => item.key === "baz");
    const resultValue = foodBar['value']; // here the extracted value is by key
    
    0 讨论(0)
  • 2020-12-15 04:57

    In the current versions of Javascript you need a loop do to it.

    However you can use a module like npm `lodash' to make it look simpler

    var _ = require('lodash')
    var permittedValues = _.pluck(array, 'key')
    

    link to pluck documentation

    0 讨论(0)
  • 2020-12-15 05:08

    Try this..

    const myArray = [
        {
            key: 'blah',
            value: 'Blah Blah'
        },
        {
            key: 'foo',
            value: 'Foos'
        },
        {
            key: 'bar',
            value: 'Bars'
        },
        {
            key: 'baz',
            value: 'Bazingo'
        }
    ];
    const resObject = myArray.find(item => item.key === "foo");
    // Here you can access object which you want
    console.log(resObject);

    Also you can refer this answer..

    Find object by id in an array of JavaScript objects

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