Returns a query string generated from the key-value pairs of the given object.

Use Array.prototype.reduce() on Object.entries(queryParameters) to create the query string.
Determine the symbol to be either ? or & based on the length of queryString and concatenate val to queryString only if it’s a string.
Return the queryString or an empty string when the queryParameters are falsy.

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

Flatten an object with the paths for keys.

Use recursion.
Use Object.keys(obj) combined with Array.prototype.reduce() to convert every leaf node to a flattened path node.
If the value of a key is an object, the function calls itself with the appropriate prefix to create the path using Object.assign().
Otherwise, it adds the appropriate prefixed key-value pair to the accumulator object.
You should always omit the second argument, prefix, unless you want every key to have a prefix.

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

Creates a function that invokes the method at a given key of an object, optionally adding any additional supplied parameters to the beginning of the arguments.

Return a function that uses Function.prototype.apply() to bind context[fn] to context.
Use the spread operator (...) to prepend any additional supplied parameters to the arguments.

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

Creates a deep clone of an object.

Use recursion.
Check if the passed object is null and, if so, return null.
Use Object.assign() and an empty object ({}) to create a shallow clone of the original.
Use Object.keys() and Array.prototype.forEach() to determine which key-value pairs need to be deep cloned.

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

Returns true if the target value exists in a JSON object, false otherwise.

Check if keys is non-empty and use Array.prototype.every() to sequentially check its keys to internal depth of the object, obj.
Use Object.prototype.hasOwnProperty() to check if obj does not have the current key or is not an object, stop propagation and return false.
Otherwise assign the key’s value to obj to use on the next iteration.
Return false beforehand if given key list is empty.

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

Applies fn to each value in arr, splitting it each time fn returns a new value.

Use Array.prototype.reduce() with an accumulator object that will hold the resulting array and the last value returned from fn.
Use Array.prototype.push() to add each value in arr to the appropriate partition in the accumulator array.

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

Applies a function against an accumulator and each key in the object (from left to right).

Use Object.keys(obj) to iterate over each key in the object, Array.prototype.reduce() to call the apply the specified function against the given accumulator.

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

Renders a table with rows dynamically created from an array of objects and a list of property names.

  • Use Object.keys(), Array.prototype.filter(), Array.prototype.includes() and Array.prototype.reduce() to produce a filteredData array, containing all objects with the keys specified in propertyNames.
  • Render a <table> element with a set of columns equal to the amount of values in propertyNames.
  • Use Array.prototype.map to render each value in the propertyNames array as a <th> element.
  • Use Array.prototype.map to render each object in the filteredData array as a <tr> element, containing a <td> for each key in the object.

This component does not work with nested objects and will break if there are nested objects inside any of the properties specified in propertyNames

继续阅读 30秒学会 React 片段 – MappedTable

Retrieve a set of properties indicated by the given selectors from an object.

Use Array.prototype.map() for each selector, String.prototype.replace() to replace square brackets with dots, String.prototype.split('.') to split each selector, Array.prototype.filter() to remove empty values and Array.prototype.reduce() to get the value indicated by it.

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