Results in a string representation of yesterday’s date.

Use new Date() to get the current date, decrement by one using Date.getDate() and set the value to the result using Date.setDate().
Use Date.prototype.toISOString() to return a string in yyyy-mm-dd format.

继续阅读 30秒学会 JavaScript 片段 – yesterday

Returns the nth element of an array.

Use Array.prototype.slice() to get an array containing the nth element at the first place.
If the index is out of bounds, return undefined.
Omit the second argument, n, to get the first element of the array.

继续阅读 30秒学会 JavaScript 片段 – nthElement

Mutates the original array to filter out the values specified, based on a given iterator function.

Check if the last argument provided in a function.
Use to apply the iterator function fn to all array elements.
Use Array.prototype.filter() and Array.prototype.includes() to pull out the values that are not needed.
Use Array.prototype.length = 0 to mutate the passed in an array by resetting it’s length to zero and Array.prototype.push() to re-populate it with only the pulled values.

继续阅读 30秒学会 JavaScript 片段 – pullBy

Returns the index of the last element for which the provided function returns a truthy value.

Use to map each element to an array with its index and value.
Use Array.prototype.filter() to remove elements for which fn returns falsy values, Array.prototype.pop() to get the last one.
-1 is the default value when not found.

继续阅读 30秒学会 JavaScript 片段 – findLastIndex

Filters out the non-unique values in an array, based on a provided comparator function.

Use Array.prototype.filter() and Array.prototype.every() for an array containing only the unique values, based on the comparator function, fn.
The comparator function takes four arguments: the values of the two elements being compared and their indexes.

继续阅读 30秒学会 JavaScript 片段 – filterNonUniqueBy

Returns the unique symmetric difference between two arrays, not containing duplicate values from either array.

Use Array.prototype.filter() and Array.prototype.includes() on each array to remove values contained in the other, then create a Set from the results, removing duplicate values.

继续阅读 30秒学会 JavaScript 片段 – uniqueSymmetricDifference

Returns true if two arrays contain the same elements regardless of order, false otherwise.

Use a for...of loop over a Set created from the values of both arrays.
Use Array.prototype.filter() to compare the amount of occurrences of each distinct value in both arrays.
Return false if the counts do not match for any element, true otherwise.

继续阅读 30秒学会 JavaScript 片段 – haveSameContents

Returns an object containing the parameters of the current URL.

Use String.prototype.match() with an appropriate regular expression to get all key-value pairs, Array.prototype.reduce() to map and combine them into a single object.
Pass as the argument to apply to the current url.

继续阅读 30秒学会 JavaScript 片段 – getURLParameters

Removes elements in an array until the passed function returns true. Returns the remaining elements in the array.

Loop through the array, using Array.prototype.slice() to drop the first element of the array until the returned value from the function is true.
Returns the remaining elements.

继续阅读 30秒学会 JavaScript 片段 – dropWhile