Mark As Completed Discussion

How to Filter an Array of Objects in JS

Before we start discussing this topic, let's do a quick revision of both arrays and objects.

At a very high level, arrays are collections of similar things. A common example you might think of is a six-pack of beer. The entire "thing" - the six-pack itself, is an array. Each individual can of beer is an item in an array.

While an array is used to group similar items, at a very high level, you might say that an object is used to describe a unique thing. The word unique here is used in the sense of "a collection of unique characteristics, grouped toghether based on some rules". In other words, an object is a collection of characteristics of a single thing, while an array is a collection of similar things.

This distinction is sometimes not obvious to a beginner programmer, but it is crucial to understand this at a conceptual level. A six-pack of beer cans might be "emulated" in JavaScript as follows:

JAVASCRIPT
1const sixPack = [
2  "beer can 1",
3  "beer can 2",
4  "beer can 3",
5  "beer can 4",
6  "beer can 5",
7  "beer can 6",
8];

So, I'm emulating a six-pack of beers as an array, where each member of that array is a string literal.