Removing the largest number from an array
To remove the largest value from an array of numbers we first need to find that number and can then filter it out.
Read full articleWith Array.prototype.reduce()
, we can extract only the properties of an object we want to pass along.
This is particularly useful when you want to send partial data to or from an API. You can pick what information you want to reveal, while keeping sensitive data hidden.
// we don’t want to pass all of this information along all the time
const user = {
email: "ckent@dailyplanet.com",
password: "i-am-superman",
username: "clarkkent"
}
// this function receives an object and a list of properties to extract
const pick = (object, props) => props.reduce((picked, prop) => ({
...picked,
[prop]: object[prop]
}), {})
// we can use `pick` to get only the information we need
const condensedUser = pick(user, ["username", "email"])
// ⇒ {
// username: "clarkkent",
// email: "ckent@dailyplanet.com"
// }
// we don’t want to pass all of this information along all the time
const user = {
email: "ckent@dailyplanet.com",
password: "i-am-superman",
username: "clarkkent"
}
// this function receives an object and a list of properties to extract
const pick = (object, props) => props.reduce((picked, prop) => ({
...picked,
[prop]: object[prop]
}), {})
// we can use `pick` to get only the information we need
const condensedUser = pick(user, ["username", "email"])
// ⇒ {
// username: "clarkkent",
// email: "ckent@dailyplanet.com"
// }
// we don’t want to pass all of this information along all the time
const user = {
email: "ckent@dailyplanet.com",
password: "i-am-superman",
username: "clarkkent"
}
// this function receives an object and a list of properties to extract
const pick = (object, props) => props.reduce((picked, prop) => ({
...picked,
[prop]: object[prop]
}), {})
// we can use `pick` to get only the information we need
const condensedUser = pick(user, ["username", "email"])
// ⇒ {
// username: "clarkkent",
// email: "ckent@dailyplanet.com"
// }
// we don’t want to pass all of this information along all the time
const user = {
email: "ckent@dailyplanet.com",
password: "i-am-superman",
username: "clarkkent"
}
// this function receives an object and a list of properties to extract
const pick = (object, props) => props.reduce((picked, prop) => ({
...picked,
[prop]: object[prop]
}), {})
// we can use `pick` to get only the information we need
const condensedUser = pick(user, ["username", "email"])
// ⇒ {
// username: "clarkkent",
// email: "ckent@dailyplanet.com"
// }
To remove the largest value from an array of numbers we first need to find that number and can then filter it out.
Read full articleAre you calling the same function many times with near-identical parameters? Hide that repetition in a higher-order function for more readability.
Read full articleWhen chaining conditions together, putting them into named variables makes code more readable.
Read full articleWe can use the ternary operator in places where doing the same thing with if-else-branches would take a lot more code.
Read full articleThe ternary operator isn’t limited to assigning values based on a condition. It also lets us switch between two near identical function calls.
Read full articleIf an array contains flat values, we can turn them into objects where they become a property’s value.
Read full article