Gets the size of an array, object or string.

Get type of val (array, object or string).
Use length property for arrays.
Use length or size value if available or number of keys for objects.
Use size of a Blob object created from val for strings.
Split strings into array of characters with split('') and return its length.

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

Groups the elements of an array based on the given function.

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 片段 – groupBy

Unflatten an object with the paths for keys.

Use Object.keys(obj) combined with Array.prototype.reduce() to convert flattened path node to a leaf node.
If the value of a key contains a dot delimiter (.), use Array.prototype.split('.'), string transformations and JSON.parse() to create an object, then Object.assign() to create the leaf node.
Otherwise, add the appropriate key-value pair to the accumulator object.

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

Renders a tree view of a JSON object or array with collapsible content.

  • Use object destructuring to set defaults for certain props.
  • Use the value of the toggled prop to determine the initial state of the content (collapsed/expanded).
  • Use the React.setState() hook to create the isToggled state variable and give it the value of the toggled prop initially.
  • Return a <div> to wrap the contents of the component and the <span> element, used to alter the component’s isToggled state.
  • Determine the appearance of the component, based on isParentToggled, isToggled, name and Array.isArray() on data.
  • For each child in data, determine if it is an object or array and recursively render a sub-tree.
  • Otherwise, render a <p> element with the appropriate style.

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

Returns a sorted array of objects ordered by properties and orders.

Uses Array.prototype.sort(), Array.prototype.reduce() on the props array with a default value of 0, use array destructuring to swap the properties position depending on the order passed.
If no orders array is passed it sort by 'asc' by default.

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

Returns the first key that satisfies the provided testing function. Otherwise undefined is returned.

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

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