JS Array Cardio - part 2

This is the seventh project of WesBos's JS30 series. To see the whole 30 part series, click here We will be creating a bunch of small exaples to play with various array methods such as some(), every(), find(), findIndex(), splice()

Here is the codepen with the starter files

So in this exercise we'll be doing some more array exercises. Our sample data -

const people = [
    { name: 'Wes', year: 1988 },
    { name: 'Kait', year: 1986 },
    { name: 'Irv', year: 1970 },
    { name: 'Lux', year: 2015 }
];

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 }
];

Array.prototype.some()

The [].some(test_fn) method tests whether at least one element in the array passes the test implemented by the provided function (test_fn). Each element of the array is iterated upon and the element is passed to test_fn, if it returns true for any one of the elements, the [].some() method ends and returns true. If test_fn returns false for each element, only then does [].some() return false.

Learn more @ MDN Docs

Task : Is at least one person 19 or older?

res = people.some(person => {
        const currentDate = (new Date()).getFullYear())
        return currentDate - person.year >= 19
    })
console.log(res) //true

Array.prototype.every()

The [].every(test_fn) method tests whether all elements in the array passes the test implemented by the provided function (test_fn). Each element of the array is iterated upon and the element is passed to test_fn, if it returns true for each one of the elements, the [].every() method returns true. If test_fn returns false for even element, [].every() returns false.

Learn more @ MDN Docs

Task : Is everyone 19 or older?

res = people.every(person => {
        const currentDate = (new Date()).getFullYear())
        return currentDate - person.year >= 19
    })
console.log(res) //false

Array.prototype.find()

The [].find(test_fn) method returns the value of the first element in the array that satisfies the provided testing function (test_fn). Otherwise undefined is returned. This works like [].some(), except that it returns the first instance of the object rather than true or false.

Learn more @ MDN Docs

Task : find the comment with the ID of 823423

const com = comments.find(comment => comment.id == 823423)
console.log(com) // { text: 'Super good', id: 823423 }

Array.prototype.findIndex()

The [].findIndex(test_fn) method returns the index of the first element in the array that satisfies the provided testing function (test_fn). Otherwise -1 is returned. Works just like [].find(), except it returns the index, not the element.

Learn more @ MDN Docs

Task : Delete the comment with the ID of 823423

const index = comments.findIndex(comment => comment.id == 823423)
const deletedComment = comments.splice(index, 1)
console.log(deletedComment)
console.log(comments)

That is it for this project. I haven't updated the codepen as there isn't much updates to see, just open the console and try this out!