JavaScript Array Methods - Checking Items

JavaScript Array Methods - Checking Items

Introduction

This is the fifth and last article in the JavaScript Array Methods series — a series created to introduce beginners to array methods, their syntax, and usage. In this article, we will discuss the array methods that are used for checking items in an array.

Prerequisites

It'll be great to read the introductory article. It gives a quick overview of array methods and why they're important. You can also check out other array methods in the series.

Array.isArray()

Sometimes, you may need to check whether an object or a value is an array. In such cases, the Array.isArray() method can be useful. It checks whether a value is an array or not, and returns either true or false.

To check if a value is an array, pass in the value as an argument in Array.isArray()

Syntax:

Array.isArray(value)

Usage:

const flowers = ["daisy", "hibiscus", "rose"]
const name = "John Doe"

console.log(Array.isArray(flowers)) // true
console.log(Array.isArray(name)) // false

indexOf()

The indexOf() array method searches for the index of a given item in an array and returns it. If the given item is not found, it returns -1. If there is more than one occurrence of the item, the index of the first occurrence will be returned.

The method takes one or two arguments. The first argument is the item to be searched and the second argument (which is optional) is the start index of the search. If a negative start index is given, the search will start counting from the end of the array.

Syntax:

array.indexOf(item)
array.indexOf(item, startIndex)

Usage:

const cars = ["BMW", "Toyota", "Honda", "Kia", "Toyota", "BMW", "Kia", "Honda"]

const findHonda = cars.indexOf("Honda")
const findToyota = cars.indexOf("Toyota", 2)
const findKia = cars.indexOf("Kia", -3)

console.log(findHonda) // 2
console.log(findToyota) // 4
console.log(findKia) // 6

lastIndexOf()

This method works like indexOf(). The difference is that it searches for the last occurrence of the given item in an array.

Syntax:

array.lastIndexOf(item)
array.lastIndexOf(item, startIndex)

Usage:

const cars = ["BMW", "Toyota", "Honda", "Kia", "Toyota", "BMW", "Kia", "Honda"]

const findHonda = cars.lastIndexOf("Honda")
const findToyota = cars.lastIndexOf("Toyota", 2)
const findKia = cars.lastIndexOf("Kia", -3)

console.log(findHonda) // 7
console.log(findToyota) // 1
console.log(findKia) // 3

find()

The find() array method searches for the first occurrence of items in an array that passes a test function and returns it. The test function of the find() method can take three arguments:

  • item - represents each item in the array

  • index - the index of the item

  • array - the array on which the find method is called

Syntax:

array.find((item) => {/*some code to execute*/})
array.find((item, index) => {/*some code to execute*/})
array.find((item, index, array) => {/*some code to execute*/})

Usage:

const testScores = [90, 85, 92, 63, 77]

const passed = testScores.find(score => score > 70)
console.log(passed) // 90

90 is given as an output because it is the first item in the array that passes the test (score is greater than 70).

If no item passes the test function, undefined is returned.

findIndex()

The findIndex() method works like find(), but instead of returning the first item in the array that passes a given test function, it returns the index of that item.

Syntax:

array.findIndex((item) => {/*some code to execute*/})
array.findIndex((item, index) => {/*some code to execute*/})
array.findIndex((item, index, array) => {/*some code to execute*/})

Usage:

const testScores = [90, 85, 92, 63, 77]

const passed = testScores.findIndex(score => score > 70)
console.log(passed) // 0

0 is given as an output because it is the index of the first item in the array that passes the test (score is greater than 70).

If no item passes the test function, -1 is returned.

includes()

This method searches an array for a particular item and returns true if it is found, and false if it is not found.

The includes() method takes two arguments: the item to be searched and the start index (where the search should start from in the array).

A positive index starts counting from the beginning, and a negative index starts counting from the end of the array.

Syntax:

array.includes(item)
array.inmcludes(item, startIndex)

Usage:

const myNames = ["Maame", "Yaa", "Serwaa", "Bona-Mensa"]

console.log(myNames.includes("Yaa")) // true
console.log(myNames.includes("Maame", 1)) // false

The second output returns false because the start index for the search is 1. This excludes “Maame” from the search.

every()

The every() array method tests if every item in the array passes a test function and returns true or false. The test function of the every() method can take three arguments:

  • item - represents each item in the array

  • index - the index of the item

  • array - the array on which the forEach method is called

Syntax:

array.every((item) => {/*some code to execute*/})
array.every((item, index) => {/*some code to execute*/})
array.every((item, index, array) => {/*some code to execute*/})

Usage:

const dice = [
    {value: 1, isHeld: true},
    {value: 5, isHeld: false},
    {value: 3, isHeld: true},
    {value: 5, isHeld: false},
    {value: 2, isHeld: true},
    {value: 6, isHeld: true}
]

const allHeldDice = dice.every(die => die.isHeld)
console.log(allHeldDice) // false

The idea for this example is from a tenzies game I built. All dice must be held to complete the game.

To implement this, I used the every() method to check the isHeld property of all the items in the array.

It returns false in the example because as you can see from the array, not all the dice are held.

every() only returns true if ALL items in the array pass the test function.

some()

What if you want to check if at least one item in the array passes a test function? You guessed right! The some() array method acts like every() but will return true if some items in the array pass the test function.

Syntax:

array.some((item) => {/*some code to execute*/})
array.some((item, index) => {/*some code to execute*/})
array.some((item, index, array) => {/*some code to execute*/})

Usage:

const dice = [
    {value: 1, isHeld: true},
    {value: 5, isHeld: false},
    {value: 3, isHeld: true},
    {value: 5, isHeld: false},
    {value: 2, isHeld: true},
    {value: 6, isHeld: true}
]

const allHeldDice = dice.some(die => die.isHeld)
console.log(allHeldDice) // true

The output is true here because at least ONE item in the array passes the test function.

length

This is not an array method. It is an array property that is used to check the length of an array, i.e. the number of items in the array. Because it is not a method, it is not followed by a bracket.

Syntax:

array.length

Usage:

const stationery = ["pen", "pencil", "eraser", "sharpener"]

console.log(stationery.length) // 4

Conclusion

We discussed in this article some array methods used for checking items in an array, their syntax and usage. They include:

  • Array.isArray() - check if a value is an array

  • indexOf() - searches for the index of the first occurrence of a given item in an array and returns it

  • lastIndexOf() - searches for the index of the last occurrence of a given item in an array and returns it

  • find() - searches for the first occurrence of items in an array that passes a test and returns it.

  • findIndex() - searches for index the first occurrence of items in an array that passes a test and returns it.

  • includes() - checks if an array includes a given item

  • every() - checks if ALL items in an array pass a test

  • some() - checks if at least ONE item in an array passes a test

  • length - checks the number of items in an array

Thanks for reading!