Groups the elements into two arrays, depending on the provided function’s truthiness for each element.

Use Array.prototype.reduce() to create an array of two arrays.
Use Array.prototype.push() to add elements for which fn returns true to the first array and elements for which fn returns false to the second one.

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

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

Loop through the array, using a for...of loop over Array.prototype.entries() until the returned value from the function is true.
Return the removed elements, using Array.prototype.slice().

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

Removes elements from the end of an array until the passed function returns true. Returns the removed elements.

Loop through the array, using a Array.prototype.reduceRight() and accumulating elements while the function returns falsy value.

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

Creates an object composed of the properties the given function returns falsy for. The function is invoked with two arguments: (value, key).

Use Object.keys(obj) and Array.prototype.filter()to remove the keys for which fn returns a truthy value.
Use Array.prototype.reduce() to convert the filtered keys back to an object with the corresponding key-value pairs.

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

Returns a random element from an array, using the provided weights as the probabilities for each element.

Use Array.prototype.reduce() to create an array of partial sums for each value in weights.
Use Math.random() to generate a random number and Array.prototype.findIndex() to find the correct index based on the array previously produced.
Finally, return the element of arr with the produced index.

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