Picking object properties
When you don’t want to drag a full object through your code, you can pick just the properties you need using reduce.
Read full articleWhen we split a string using a sequence of characters, the string we split at no longer appears in the result.
If we split at every empty string, we get the individual characters, including spaces.
// If we split at “space”, no spaces appear in the result:
"two words".split(" ") // ⇒ ["two", "words"]
// Periods and other characters still remain:
"This is a sentence.".split(" ") // ⇒ ["This", "is", "a", "sentence."]
// If we split at a letter, that letter is removed. The space remains:
"hello world".split("r") // ⇒ ["hello wo", "ld"]
// If we split at “empty space”, the string is split into all of its
// characters. The space is still in there:
"oh yeah".split("") // ⇒ ["o", "h", " ", "y", "e", "a", "h"]
// If we split at “space”, no spaces appear in the result:
"two words".split(" ") // ⇒ ["two", "words"]
// Periods and other characters still remain:
"This is a sentence.".split(" ") // ⇒ ["This", "is", "a", "sentence."]
// If we split at a letter, that letter is removed. The space remains:
"hello world".split("r") // ⇒ ["hello wo", "ld"]
// If we split at “empty space”, the string is split into all of its
// characters. The space is still in there:
"oh yeah".split("") // ⇒ ["o", "h", " ", "y", "e", "a", "h"]
// If we split at “space”, no spaces appear in the result:
"two words".split(" ") // ⇒ ["two", "words"]
// Periods and other characters still remain:
"This is a sentence.".split(" ") // ⇒ ["This", "is", "a", "sentence."]
// If we split at a letter, that letter is removed. The space remains:
"hello world".split("r") // ⇒ ["hello wo", "ld"]
// If we split at “empty space”, the string is split into all of its
// characters. The space is still in there:
"oh yeah".split("") // ⇒ ["o", "h", " ", "y", "e", "a", "h"]
// If we split at “space”, no spaces appear in the result:
"two words".split(" ") // ⇒ ["two", "words"]
// Periods and other characters still remain:
"This is a sentence.".split(" ") // ⇒ ["This", "is", "a", "sentence."]
// If we split at a letter, that letter is removed. The space remains:
"hello world".split("r") // ⇒ ["hello wo", "ld"]
// If we split at “empty space”, the string is split into all of its
// characters. The space is still in there:
"oh yeah".split("") // ⇒ ["o", "h", " ", "y", "e", "a", "h"]
When you don’t want to drag a full object through your code, you can pick just the properties you need using reduce.
Read full articleWe can reduce an array of numbers into a single value that represents the sum of its values, without needing a traditional for-loop.
Read full articleSince array methods like filter and map return an array themselves, we can chain them one after the other.
Read full articleTo asynchronously fetch data in a React component using hooks, we can define and then call an asynchronous function inside of useEffect.
Read full articleWe can write a function that gives us the array object with the largest value for a specified property.
Read full articleWe sometimes need a type in which only one of two mutually exclusive properties can be set. Setting either property is fine, setting both isn’t.
Read full article