Overview
- The .filter() method returns a new array that contains some of the items from the original array, based on the condition you specify.
- JavaScript will take your callback function and call it for every single item in the array.
- For the .filter() method, the result of the callback function matters. When it's true, the item will be included in the resulting array. Otherwise, it won't.
- JavaScript cannot make a smart guess that the numbers array becomes the number parameter in your callback function. What it does is that it calls your callback function while giving a value for the first parameter that you specified.
- Use the plural -> singular naming convention when using the .filter() method.
Methods learned
- .find() -finds items.
- .filter() - filters out items.
- .map() - transforms an array into another one.
- .includes() - returns a boolean
- .
Return positive numbers in an array
function getPositiveTemperatures(temperatures) {
const positiveNumbers = temperatures.filter(function(temp) {
return temp > 0;
});
return positiveNumbers
}
// Sample usage - do not modify
console.log(getPositiveTemperatures([-5, 12, 3])); // [12, 3]
console.log(getPositiveTemperatures([1, -3, -2, 4, 10])); // [1, 4, 10]
And returning negative numbers in an array
function getFreezingTemperatures(temperatures) {
const negativeNumbers = temperatures.filter(function(temp) {
return temp < 0;
});
return negativeNumbers// [14, 11]
}
// Sample usage - do not modify
console.log(getFreezingTemperatures([-5, 12, 3])); // [-5]
console.log(getFreezingTemperatures([1, -3, -2, 4, 10])); // [-3, -2]
Difference between .filter() and .find()
.filter() always returns an array - even if it is empty .find() returns the first array item. Returns undefined if that item is not found.
return a specific item from the array
function getYear(years, searchYear) {
const result = years.find(function(item) {
return item === searchYear;
});
return result;
}
// Sample usage - do not modify
console.log(getYear([2019, 2020, 2021], 2020)); // 2020
console.log(getYear([2019, 2020, 2021], 1990)); // undefined
Return odd years
function getOddYears(years) {
const result = years.filter(function(year) {
if(year % 2 !== 0){
return year
}
});
return result;
}
//or, remembering the early return
function getOddYears(years) {
return years.filter(function(year) {
return year % 2 !== 0;
});
Basic includes
function isAppUsed(apps, app) {
return apps.includes(app)
}
Return string length from array
Gotta start learning how to simplify down the code
function getStringSizes(strings) {
const toNumber = strings.map(function(string) {
return string.length;
});
return toNumber;
}
//or
function getStringSizes(strings) {
return strings.map(function(string) {
return string.length;
})
}
Classroom Grade project
/** @param {number[]} grades */
export function getNumberOfGrades(grades) {
// TODO: return the number of grades
return grades.length
}
/** @param {number[]} grades */
export function getSumGrades(grades) {
// TODO: return the sum of all the grades
let total = 0
const all = grades.forEach(function(grade){
return total += grade
});
return total
}
/** @param {number[]} grades */
export function getAverageValue(grades) {
// TODO: return the average value of all grades (sum of all grades divided by the total number of grades)
//get sum
let averageGrades = getSumGrades(grades) / getNumberOfGrades(grades)
return averageGrades;
}
/** @param {number[]} grades */
export function getPassingGrades(grades) {
// TODO: return all passing grades (10 and above)
return grades.filter(function(grade) {
return grade >=10;
});
}
/** @param {number[]} grades */
export function getFailingGrades(grades) {
// TODO: return all failing grades (9 and below)
return grades.filter(function(grade) {
return grade <=9;
});
}
/** @param {number[]} grades */
export function getRaisedGrades(grades) {
// TODO: return all the grades raised by 1 (no grade should exceed 20)
let gradePlus = 0;
return grades.map(function(grade){
if(grade !== 20){
return grade + 1
}
return grade;
});
}
Previewing the Fetch function
Returns all citizens that can vote
export function getVotersCount(ages) {
console.log(ages);
const newArray = ages.filter(function(age) {
return age >= 18;
});
console.log(newArray.length) //not needed
return newArray.length;
}
//or with chaining....
export function getVotersCount(ages) {
return ages.filter(function(age){
return age >= 18;
}).length;
}
#Review
- The .filter() method returns a new array that contains some of the items from the original array, based on the condition you specify.
- JavaScript will take your callback function and call it for every single item in the array.
- For the .filter() method, the result of the callback function matters. When it's true, the item will be included in the resulting array. Otherwise, it won't.
- JavaScript cannot make a smart guess that the numbers array becomes the number parameter in your callback function. What it does is that it calls your callback function while giving a value for the first parameter that you specified.
- Use the plural -> singular naming convention when using the .filter() method.
- The .find() method returns the first item which matches the condition that you specify. If no items were found, you will get back undefined.
- The .filter() method always returns an array. Even if it's empty.
- The array .map(callback) method allows you to transform an array into another one.
- The array .includes(item) method takes an item and returns true when that item exists in the array and false otherwise.
- The array .join(glue) method returns a string of the array elements separated by the glue.