7 Useful JavaScript Array and Object Methods


Today we’re taking a look at some useful array and object methods for JavaScript. Throughout this entire post, we’re going to be using this object named books of, you guessed it, book objects to test out some methods:

const books = [
   {title: 'Dune',              rating: 5,  price: 56 },
   {title: 'The Stand',         rating: 3,  price: 74 },
   {title: 'I Am Legend',       rating: 4,  price: 16 },
   {title: 'Childhood\'s End',  rating: 5,  price: 18 },
   {title: 'AKOFAF',            rating: 2,  price: 10 },
   {title: 'Ender\'s Game',     rating: 5,  price: 24 },
]

Filter method

If we want to see only books rated 3-stars and up, we can use the filter methods for this:

const filteredBooks = books.filter((book) => {
    return  book.rating >= 3
})

console.log(filteredBooks);

Logs in the following object in the console:

[
    {"title": "Dune", "rating": 5, "price": 56},
    {"title": "The Stand", "rating": 3, "price": 74},
    {"title": "I Am Legend", "rating": 4, "price": 16},
    {"title": "Childhood's End", "rating": 5, "price": 18},
    {"title": "Ender's Game", "rating": 5," price": 24}
]

Map method

Let’s say we want to see all the books in our collection by title, we can use the map method to list the titles like this:

const bookTitles = books.map((book) => {
    return  book.title
})

console.log(bookTitles);

The console returns all the titles in the books:

[
    "Dune",
    "The Stand",
    "I Am Legend",
    "Childhood's End",
    "AKOFAF",
    "Ender's Game"
]

Find method

The find method allows us to find and return a single book in our book object. Let’s search for Dune:

const foundBook = books.find((book) => {
    return  book.title === 'Dune'
})

console.log(foundBook);

We search by the title of Dune with the case-sensitive string and we are returned all its types title, rating, and price:

{
    "title": "Dune",
    "rating": 5,
    "price": 56
}

forEach method

If we want to see all the book titles in our object again, we could use the forEach method

books.forEach((book) => {
    console.log(book.title)
})

This returns the following in console:

Dune
The Stand
I Am Legend
Childhood's End
AKOFAF
Ender's Game

Some method

Perhaps we want to check if any of our books have a 2-star rating or less.

The some method will check our object and as soon as one book has a rating of 2 or less it’ll return true, if it checks the entire array and there are no books that match this condition it’ll return false.

const hasLowRatingBooks = books.some((book) => {
    return book.rating <= 2
})

console.log(hasLowRatingBooks);

In this case, the function checked the entire object, and the console returns true since at least one of our books titled AKOFAF has a 2-star rating.

Every method

The every method is similar to the some method above, but instead of checking for only one book to meet the condition, it checks to make sure every book meets the condition.

Let’s check to see if all our books have at least a 3-star rating and higher :

const hasGoodRatedBooks = books.every((book) => {
    return book.rating >= 3
})

console.log(hasGoodRatedBooks);

The above method returns false since one book has a 2-star rating.

Reduce method

We can get the total price of all the books in our object using the reduce method as follows:

const totalPrice = books.reduce((currentTotal, book) => {
    return book.price + currentTotal
}, 0)

console.log(totalPrice)

The reduce method runs the first time and returns 0 as the currentTotal. Then it runs again and adds the price of Dune to the currentTotal, and iterates through every book in the object, adding the new book price to the current total of the books that came before it until it iterates through the entire object, leaving us with the final currentTotal of all the books.

Resources:


Leave a Reply

Your email address will not be published. Required fields are marked *