- People array
-
const people = [
{ name: 'Wes', year: 1988 },
{ name: 'Kait', year: 1986 },
{ name: 'Irv', year: 1970 },
{ name: 'Lux', year: 2015 }
];
- People: Is at least one person 19 or older? Is everyone 19 or older?
-
const grownUp = el => (new Date().getFullYear() - el.year) >= 19;
people.some(grownUp); // true
people.every(grownUp); // false
// or:
people.some(el => (new Date().getFullYear() - el.year) >= 19); // true
people.every(el => (new Date().getFullYear() - el.year) >= 19); // false
- Comments array
-
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 }
];
- Comments: Find the comment with the ID of 823423. Find the index.
-
comments.find(el => el.id === 823423); // {text: 'Super good', id: 823423}
comments.findIndex(el => el.id === 823423); // 1
- Comments: Delete the comment with ID of 823423
-
const index = comments.findIndex(el => el.id === 823423); // 1
comments.splice(index, 1);
// or just:
comments.splice(comments.findIndex(el => el.id === 823423), 1);