Array methods can be used on an array of objects with relative ease with one nuance – accounting for object properties. The solution is to utilize dot notation.
A simple array of objects is provided below as a demonstration.
The some() array method is used when you are trying to determine whether at least one of the items in the array passes a certain condition. The output will return ‘true’ or ‘false’. In the example below, the ‘some’ array method is checking to determine whether any of the array of objects has a guest with the age property of greater than 21 years.
The every() array method similarly checks to determine whether any item meets the established condition. In this case, the ‘every’ method is checking to determine whether ‘every’ one of the array of objects has a guest with an age property greater than 21 years. The output will return ‘true’ or ‘false’.
The find() array method simply finds the specified item and returns it. In the case below, it returns the entire object that includes the specified property.
In addition, the methods can be combined. The example below demonstrates the combination of the filter(), includes(), and map() methods. The filter() array method loops through the list to find the condition which it was passed, producing the new array with only the items that meet that condition. In the case below, the ‘filter’ method was combined with the ‘includes’ method, which normally would produce a ‘true’ or ‘false’ result, to ‘filter’ through the items, finding the ‘music’ property on each object. Then, a new array with the conditioned items are rendered with the ‘map’ method.
The forEach() array method is useful when you want to display the entire array, or, in this case, the array of objects. The method loops through every item, rendering the entire guest list.