I am trying to find a way to select multiple elements from an array that share the same value. When I use array.find(), it only returns the first element that matches the condition. For example, in the code below, only "Donald Trump" is displayed in the console:
const data = [
{
"position": "president",
"name": "Donald Trump",
"language": "english"
},
{
"position": "president",
"name": "Vladimir Putin",
"language": "russian"
},
{
"position": "king",
"name": "Shutruk-Nahhunte",
"language": "elamite"
},
];
let result = data.find(elem => elem.position == "president");
console.log(result.name);
However, I need to retrieve all the matching values as an array, like this:
[
"Donald Trump",
"Vladimir Putin"
]
Could someone provide guidance on how to achieve this efficiently, especially considering that the actual array is quite large? Thank you for any assistance!