Compares two objects to determine if the first one contains equivalent property values to the second one, based on a provided function.

Use Object.keys(source) to get all the keys of the second object, then Array.prototype.every(), Object.hasOwnProperty() and the provided function to determine if all keys exist in the first object and have equivalent values.
If no function is provided, the values will be compared using the equality operator.

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

Creates an object with the same keys as the provided object and values generated by running the provided function for each value.

Use Object.keys(obj) to iterate over the object’s keys.
Use Array.prototype.reduce() to create a new object with the same keys and mapped values using fn.

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

Converts a comma-separated values (CSV) string to a 2D array of objects.
The first row of the string is used as the title row.

Use Array.prototype.slice() and Array.prototype.indexOf('\n') and String.prototype.split(delimiter) to separate the first row (title row) into values.
Use String.prototype.split('\n') to create a string for each row, then and String.prototype.split(delimiter) to separate the values in each row.
Use Array.prototype.reduce() to create an object for each row’s values, with the keys parsed from the title row.
Omit the second argument, delimiter, to use a default delimiter of ,.

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

Returns the target value in a nested JSON object, based on the keys array.

Compare the keys you want in the nested JSON object as an Array.
Use Array.prototype.reduce() to get value from nested JSON object one by one.
If the key exists in object, return target value, otherwise, return null.

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

Inverts the key-value pairs of an object, without mutating it. The corresponding inverted value of each inverted key is an array of keys responsible for generating the inverted value. If a function is supplied, it is applied to each inverted key.

Use Object.keys() and Array.prototype.reduce() to invert the key-value pairs of an object and apply the function provided (if any).
Omit the second argument, fn, to get the inverted keys without applying a function to them.

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

Given a flat array of objects linked to one another, it will nest them recursively.
Useful for nesting comments, such as the ones on

Use recursion.
Use Array.prototype.filter() to filter the items where the id matches the link, then to map each one to a new object that has a children property which recursively nests the items based on which ones are children of the current item.
Omit the second argument, id, to default to null which indicates the object is not linked to another one (i.e. it is a top level object).
Omit the third argument, link, to use 'parent_id' as the default property which links the object to another one by its id.

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

Iterates over all own properties of an object, running a callback for each one.

Use Object.keys(obj) to get all the properties of the object, Array.prototype.forEach() to run the provided function for each key-value pair. The callback receives three arguments – the value, the key and the object.

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