Groups the elements of an array based on the given function and returns the count of elements in each group.

Use Array.prototype.map() to map the values of an array to a function or property name.
Use Array.prototype.reduce() to create an object, where the keys are produced from the mapped results.

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

Assigns default values for all properties in an object that are undefined.

Use Object.assign() to create a new empty object and copy the original one to maintain key order, use Array.prototype.reverse() and the spread operator ... to combine the default values from left to right, finally use obj again to overwrite properties that originally had a value.

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

Omits the key-value pairs corresponding to the given keys from an object.

Use Object.keys(obj), Array.prototype.filter() and Array.prototype.includes() to remove the provided keys.
Use Array.prototype.reduce() to convert the filtered keys back to an object with the corresponding key-value pairs.

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

Creates a function that invokes fn with a given context, optionally adding any additional supplied parameters to the beginning of the arguments.

Return a function that uses Function.prototype.apply() to apply the given context to fn.
Use Array.prototype.concat() to prepend any additional supplied parameters to the arguments.

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

Creates a new object from the specified object, where all the keys are in lowercase.

Use Object.keys() and Array.prototype.reduce() to create a new object from the specified object.
Convert each key in the original object to lowercase, using String.toLowerCase().

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

Given an array of valid property identifiers and an array of values, return an object associating the properties to the values.

Since an object can have undefined values but not undefined property pointers, the array of properties is used to decide the structure of the resulting object using Array.prototype.reduce().

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

Converts an array of objects to a comma-separated values (CSV) string that contains only the columns specified.

Use Array.prototype.join(delimiter) to combine all the names in columns to create the first row.
Use Array.prototype.map() and Array.prototype.reduce() to create a row for each object, substituting non-existent values with empty strings and only mapping values in columns.
Use Array.prototype.join('\n') to combine all rows into a string.
Omit the third argument, delimiter, to use a default delimiter of ,.

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

Deep maps an object’s keys.

Creates an object with the same values as the provided object and keys generated by running the provided function for each key.
Use Object.keys(obj) to iterate over the object’s keys.
Use Array.prototype.reduce() to create a new object with the same values and mapped keys using fn.

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

Performs a deep comparison between two values to determine if they are equivalent.

Check if the two values are identical, if they are both Date objects with the same time, using Date.getTime() or if they are both non-object values with an equivalent value (strict comparison).
Check if only one value is null or undefined or if their prototypes differ.
If none of the above conditions are met, use Object.keys() to check if both values have the same number of keys, then use Array.prototype.every() to check if every key in the first value exists in the second one and if they are equivalent by calling this method recursively.

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