Returns an array of function property names from own (and optionally inherited) enumerable properties of an object.

Use Object.keys(obj) to iterate over the object’s own properties.
If inherited is true, use Object.get.PrototypeOf(obj) to also get the object’s inherited properties.
Use Array.prototype.filter() to keep only those properties that are functions.
Omit the second argument, inherited, to not include inherited properties by default.

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

Returns a string by concatenating all of the elements in the collection, separated by the specified separator string.

Use reflect.ValueOf() to get the array or slice and the separator string, make() to create an appropriate string slice.
Use a for loop with Value.Len() and Value.Index() to iterate over each element, fmt.Sprintf() to convert it to a string.
Use strings.Join() to combine the strings using the separator provided.

继续阅读 30秒学会 Golang 片段 – Join

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

Creates a new object from the combination of two or more objects.

Use Array.prototype.reduce() combined with Object.keys(obj) to iterate over all objects and keys.
Use hasOwnProperty() and Array.prototype.concat() to append values for keys existing in multiple objects.

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