The group() method groups the elements of the calling array according to the string values returned by a provided testing function. The returned object has separate properties for each group, containing arrays with the elements in the group.
The string in JavaScript can be converted into a character array by using the split() and Array. from() functions.
JavaScript calls the toString method automatically when an array is to be represented as a text value or when an array is referred to in a string concatenation.
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Let's say I have a collection of different URLs in an array:
var source = ['www.xyz.com/Product/1', 'www.xyz.com/Product/3', 'www.xyz.com/Category/1', 'somestring']
What would be a good way to iterate over the array and group similar strings into a separate array? The desired output from the example above would be:
var output = [
['www.xyz.com/Product/1', 'www.xyz.com/Product/3'],
['www.xyz.com/Category/1'],
['somestring']
];
Conditions
source
can be random stringsI found the string-similarity library which gives the possibility to compare one string against a collection of strings. One way would be to iterate over the source, compare each item against the source collection and apply a rule to group items with a similar score. However I guess this would be terrible inefficient.
Can someone suggest me an efficient way to accomplish what I need?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With