Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group similar strings from an array in Node.js

Tags:

People also ask

How do you group an array?

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.

Can strings be treated like arrays in JavaScript?

The string in JavaScript can be converted into a character array by using the split() and Array. from() functions.

Which method is used to convert arrays to strings in JavaScript?

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.

What is array in node JS?

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

  • All items within source can be random strings
  • The logic must be able to compare and group around 100'000 items in a meaningful time

I 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?