This is day 7 in my #javascript30 journey. This is the free course from Wes Bos that lets you brush up on your JavaScript skills via 30 projects.
Yesterday we created an awesome Type Ahead predictor. You can keep track of all the projects we’re building here.
Today we’re working on our array foo again with Array Cardio Day 2
.
Via GIPHY
Day 7 - Cardio Day 2
Today we are looking into additional Array methods including some, every, find, and findIndex.
Working with People
Calm down, we’re not working with actual people here. Just an array containing people:
const people = [
{ name: 'Wes', year: 1988 },
{ name: 'Kait', year: 1986 },
{ name: 'Irv', year: 1970 },
{ name: 'Lux', year: 2015 }
];
First we will use some
to check if at least one person is 19 or older:
const isAdult = people.some(person => {
const currentYear = new Date().getFullYear()
return currentYear - person.year >= 19
})
console.log({ isAdult })
We are able to simplify this further. I will do this in the next tasks that has us using every
to see whether all of the people are 19 or older:
const allAdult = people.every(
person => new Date().getFullYear() - person.year >= 19,
)
console.log({ allAdult })
This puts all of the elements into the arrow function and relies on the implicent return.
For the last couple of tasks we are going to be working with an array of comments:
const comments = [
{ text: 'Love this!', id: 523423 },
{ text: 'Super good', id: 823423 },
{ text: 'You are the best', id: 2039842 },
{ text: 'Ramen is my fav food ever', id: 123523 },
{ text: 'Nice Nice Nice!', id: 542328 },
]
Using the the find
method on this array we can find the comment with the ID of 823423:
const findElement = comments.find(comment => comment.id === 823423)
console.log(findElement)
Using the findIndex
method we can find and delete the comment with the id of 823423:
const elementID = comments.findIndex(comment => comment.id === 823423)
comments.splice(elementID, 1)
console.log(comments)
That’s the basis of this entire array cardio day. It was a good chance to work with some array methods that I hadn’t explored recently.
There’s no project worth publishing from today it was a good chance to work those array methods. Time for a cooldown.
Via GIPHY
You can keep track of all the projects in this JavaScript30 challenge here.