Returns the index of the first element in the provided collection that satisfies the provided testing function, -1 otherwise.

Implement an appropriate function for each type.
Use range to iterate over elements in the given collection, using f to determine if the element matches.
Return the index of the first matching element or -1 if none is found.

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

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