I have an array populated with various objects containing different key-value pairs. I need to extract specific values from the array and determine if a certain value is present or not.
function groupByName (contract) {
const { age } = contract;
const groups = [
{name: 'John', age: 30},
{name: 'Jack', age: 33},
{name: 'Tom', age: 40}
...
];
...
}
Currently, in order to compare the age
in the groups
array, I rely on loop functions to iterate through each object one by one.
groups.forEach(g => {
if (g.age === age) {
...
} else {
...
}
});
However, I find this method cumbersome and am seeking a simpler and more efficient solution. Any suggestions would be greatly appreciated!