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

javascript - Get key value based on value of another key in object - Stack Overflow

programmeradmin0浏览0评论

I have a case I keep coming across where I need to get just an object key - not the entire object - based on another key value in the same object, all from an array of objects.

So for example, if I have the following array of objects:

myArray = [
  {
    name: Person 1
    type: alpha
  },
  {
    name: Person 2
    type: beta
  },
  {
    name: Person 3
    type: gamma
  },
  {
    name: Person 4
    type: beta
  },
  {
    name: Person 5
    type: gamma
  },
];

So if I want to get just the name values for those objects with a type of 'beta', how would I do that? I prefer lodash, and I know how to use _.map or _.filter, e.g.

var newArray = _.map(myArray, function(item) {
  return item.type === 'beta';
});

but those return the whole object. I suspect I can get what I want with chaining, but I'm not figuring out how I can do this.

Thanks.

I have a case I keep coming across where I need to get just an object key - not the entire object - based on another key value in the same object, all from an array of objects.

So for example, if I have the following array of objects:

myArray = [
  {
    name: Person 1
    type: alpha
  },
  {
    name: Person 2
    type: beta
  },
  {
    name: Person 3
    type: gamma
  },
  {
    name: Person 4
    type: beta
  },
  {
    name: Person 5
    type: gamma
  },
];

So if I want to get just the name values for those objects with a type of 'beta', how would I do that? I prefer lodash, and I know how to use _.map or _.filter, e.g.

var newArray = _.map(myArray, function(item) {
  return item.type === 'beta';
});

but those return the whole object. I suspect I can get what I want with chaining, but I'm not figuring out how I can do this.

Thanks.

Share Improve this question edited Sep 16, 2019 at 21:05 SS_SS 435 bronze badges asked Mar 13, 2017 at 4:37 wonder95wonder95 4,2558 gold badges50 silver badges82 bronze badges 2
  • What form do you want the result in? Array of strings? – S McCrohan Commented Mar 13, 2017 at 4:40
  • @SMcCrohan Yes, that would be the desired result. – wonder95 Commented Mar 13, 2017 at 4:41
Add a comment  | 

4 Answers 4

Reset to default 13

You can do this with the native Array.prototype.map(). It'd look like this (using ES6 fat-arrow functions for conciseness):

myArray.filter(item => item.type === 'beta').map(item => item.name)

The ES5 form is:

myArray.filter(function(item) {return item.type === 'beta'})
     .map(function(item) {return item.name})

Here's a lodash solution that uses map and filter.

var result = _(myArray).filter({ type: 'beta' }).map('name').value();

var myArray = [
  {
    name: 'Person 1',
    type: 'alpha'
  },
  {
    name: 'Person 2',
    type: 'beta'
  },
  {
    name: 'Person 3',
    type: 'gamma'
  },
  {
    name: 'Person 4',
    type: 'beta'
  },
  {
    name: 'Person 5',
    type: 'gamma'
  },
];

var result = _(myArray).filter({ type: 'beta' }).map('name').value();

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

You can use filter method to return the items of type 'beta', then use the map method to return only the item.name of each object:

myArray.
   filter(function(item){ return item.type === 'beta'; }).
   map(function(item){ return item.name; });

ES6 with parameter destructuring:

myArray.filter(({type}) => type === 'beta').map(({name}) => name)
发布评论

评论列表(0)

  1. 暂无评论