I often encounter a situation where I need to extract just an object key, rather than the entire object, based on another key value in the same object from an array of objects.
For instance, consider 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
},
];
If I want to retrieve only the name values for objects with a type of 'beta', how can I accomplish that? I am partial to lodash and familiar with using _.map or _.filter, such as:
var newArray = _.map(myArray, function(item) {
return item.type === 'beta';
});
However, these methods return the entire object. I believe chaining may hold the solution, but I am struggling to identify how to achieve my desired outcome.
Thank you.