A few small functions written in JavaScript may help you 2

Tram Ho

unzip

Use Array.prototype.map () to turn each element into an array. Use Array.prototype.reduce () and Array.prototype.forEach () to map the values ​​grouped into individual arrays.

frequencies

Returns an object with unique values ​​of an array and the number of occurrences of values.

Use Array.prototype.reduce () to map unique values ​​to the object’s keys, in addition to the existing keys each time they meet the same value.

mostFrequent

Returns the element that appears the most in an array.

Use Array.prototype.reduce () to map unique values ​​to the object’s keys, in addition to the existing keys each time the same value is encountered. Use Object.entries () on the result in combination with Array.prototype.reduce () to get the value that appears the most in the array.

isContainedIn

Returns true if the elements of the first array are contained in the second part regardless of the order or difference.

Use the for … of loop for the Set Set created from the first array. Use Array.prototype.some () to check if all individual values ​​are contained in the second array, use Array.prototype.filter () to compare the number of occurrences of each value. separately in both arrays. Returns false if the number of any elements is greater in the first array than the second, otherwise, true.

haveSameContents

Returns true if the two arrays contain the same elements regardless of order or difference.

Using a for loop for a Set Set is made up of values ​​of both arrays. Use Array.prototype.filter () to compare the number of occurrences of each individual value in both arrays. Returns false if the total does not match any elements, otherwise returns true

weightedSample

Returns a random element from an array, using the provided weights as the probability for each element.

Use Array.prototype.reduce () to create a partial partial array for each weighted value. Use Math.random () to generate random numbers and Array.prototype.findIndex () to find the exact index based on the previously created array. Finally, return the element of the array with the index produced.

JSONtoCSV

Converts an array of objects into comma separated values ​​(CSV) strings that contain only specified columns.

Use Array.prototype.join (delimiters) to combine all the names in the columns to create the first row. Use Array.prototype.map () and Array.prototype.reduce () to create a row for each object, replace the non-existent values ​​with empty strings, and only map values ​​in the columns. Use Array.prototype.join (‘ n’) to combine all rows into a string.

countBy

Group the elements of an array based on the given function and return the number of elements in each group.

Use Array.prototype.map () to map the values ​​of an array to function names or properties. Use Array.prototype.reduce () to create an object, where keys are generated from mapping results.

differenceBy

Returns the difference between the two arrays, after applying the function provided to each element of both arrays.

Create the Set Set by applying fn to each element in b, then using Array.prototype.map () to apply fn to each element in a, then Array.prototype.filter ()

Share the news now

Source : Viblo