Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Array Cardio Day 1
- // Array.prototype.filter()
- // 1. Filter the list of inventors for those who were born in the 1500's
- const fifteen = inventors.filter(inventor => {
- if(inventor.year >= 1500 && inventor.year < 1600){
- return true
- }
- })
- // Array.prototype.map()
- // 2. Give us an array of the inventors' first and last names
- inventors.map(inventor => {
- return inventor.first + " " + inventor.last
- })
- // Array.prototype.sort()
- // 3. Sort the inventors by birthdate, oldest to youngest
- inventors.sort((a, b) => a.year > b.year ? 1 : -1)
- // 5. Sort the inventors by years lived
- const inventorVitality = inventors.sort((a, b) => {
- lastGuy = a.passed - a.year
- nextGuy = b.passed - b.year
- return lastGuy > nextGuy ? 1 : -1
- })
- // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
- const category = document.querySelector('.mw-category')
- const links = [...(category.querySelectorAll('a'))]
- const de = links
- .map(link => link.textContent)
- .filter(srteetName => srteetName.includes('de'))
- // 7. sort Exercise
- // Sort the people alphabetically by last name
- const sortingPeople = people.sort((lastOne, nextOne) => {
- const [aLast, aFirst] = lastOne.split(", ");
- const [bLast, bFirst] = nextOne.split(", ");
- return aLast > bLast ? 1 : -1
- })
- // 8. Reduce Exercise
- // Sum up the instances of each of these
- const transporting = data.reduce((obj, item) => {
- if (!obj[item]) {
- obj[item] = 0;
- }
- obj[item]++;
- return obj;
- }, {});
- Array Cardio Day 2
- // Array.prototype.some() // is at least one person 19 or older
- people.some(person => {
- var currentYear = (new Date()).getFullYear();
- return currentYear - person.year
- })
- // Array.prototype.every() // is everyone 19 or older?
- people.every(person => {
- ( (new Date()).getFullYear() ) - person.year >= 19
- })
- // Array.prototype.find()
- // Find is like filter, but instead returns just the one you are looking for
- // find the comment with the ID of 823423
- comments.find(comment => comment.id === 823423 )
- // Array.prototype.findIndex()
- // Find the comment with this ID
- const index = comments.findIndex(comment => comment.id === 823423)
Add Comment
Please, Sign In to add comment