最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - lodash: get an object value from an object array - Stack Overflow

programmeradmin0浏览0评论

I have an object array like the following. Each object in the array has an instructors field which is also an array. How can I get all email fields from this object array via lodash?

Do I need to use a double _.map function? I can run a foreach in the object and then another foreach in the instructors but I don't think that's very elegant. I can't wrap my head around getting values from object arrays that contain other array fields. Any help would be greatly appreciated.

[
{
    'title': 'New Class',
    'instructors': [
        {
            'email': '[email protected]'
        },
        {
            'email': '[email protected]'
        }    
    ]
},
{
    'title': 'New Class 2',
    'instructors': [
        {
            'email': '[email protected]'
        },
        {
            'email': '[email protected]'
        }    
    ]
}    

];

I have an object array like the following. Each object in the array has an instructors field which is also an array. How can I get all email fields from this object array via lodash?

Do I need to use a double _.map function? I can run a foreach in the object and then another foreach in the instructors but I don't think that's very elegant. I can't wrap my head around getting values from object arrays that contain other array fields. Any help would be greatly appreciated.

[
{
    'title': 'New Class',
    'instructors': [
        {
            'email': '[email protected]'
        },
        {
            'email': '[email protected]'
        }    
    ]
},
{
    'title': 'New Class 2',
    'instructors': [
        {
            'email': '[email protected]'
        },
        {
            'email': '[email protected]'
        }    
    ]
}    

];

Share Improve this question asked Mar 29, 2016 at 15:49 AlistairAlistair 6411 gold badge7 silver badges22 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Do I need to use a double _.map function?

That's one solution. You believe you are looking for flatMap:

var classes = [{
  'title': 'New Class',
  'instructors': [{
    'email': '[email protected]'
  }, {
    'email': '[email protected]'
  }]
}, {
  'title': 'New Class 2',
  'instructors': [{
    'email': '[email protected]'
  }, {
    'email': '[email protected]'
  }]
}];

var emails = _.flatMap(classes, function(cls) {
  return _.map(cls.instructors, 'email');
});

document.querySelector('#out').innerHTML = JSON.stringify(emails, null, 4);
<script src="https://cdn.jsdelivr/lodash/4.6.1/lodash.min.js"></script>
<pre id="out"></pre>

So you know, the vanilla method is quite short too:

var out = arr.reduce(function (p, c) {
  return p.concat(c.instructors.map(function (instructor) {
    return instructor.email;
  }));
}, []);

This should work :

var allEmails = [];

_.each(myArray, function(obj) {
  _.each(obj.instructors, function(instructor) {
    allEmails.push(instructor.email);
  }, this);
}, this);

return allEmails;

https://jsfiddle/k4uahqkk/1/

A more elegant solution using _.reduce and _.map would be :

_.reduce(myArray, function(result, value, key) {
    return result.concat(_.map(value.instructors, 'email'));
}, []);

https://jsfiddle/z1tg4tro/4/

edit: _.pluck since v4.x is deprecated, use _.map instead.

发布评论

评论列表(0)

  1. 暂无评论