JavaScript: 检测和删除相似字符串的算法

JavaScript: algorithm of detect and remove similar strings

假设我有一个如下所示的 JS 数组:

0 - The Big Bang Theory - Fourth Season
1 - The Big Bang Theory - Third Season
2 - The Big Bang Theory - Second Season
3 - The Big Bang Theory - First Season
4 - The Big Bang Theory - First Season (2007)
5 - The Big Bang Theory - Fourth Season (2010)
6 - The Big Bang Theory - Second Season (2008)
7 - The Big Bang Theory - Third Season (2009)
8 - The Big Bang Theory: Access All Areas (2012)
9 - The Big Bang Theory: It All Started with a Big Bang (2012)

而且我们知道有些商品是相似的。输出应该类似于下面的数组:

0 - The Big Bang Theory - Fourth Season
1 - The Big Bang Theory - Third Season
2 - The Big Bang Theory - Second Season
3 - The Big Bang Theory - First Season
8 - The Big Bang Theory: Access All Areas (2012)
9 - The Big Bang Theory: It All Started with a Big Bang (2012)

如何才能省略类似的项目?你有什么解决办法?

谢谢

您可以从每个标题中删除括号中的位,将其转储到一个集合中——这会消除重复项,然后将其转回数组:

movies = [...new Set(movies.map(movie => movie.replace(/\s*\(\d+\)\s*$/g, '')))];

movies = [
'The Big Bang Theory - Fourth Season',
'The Big Bang Theory - Third Season',
'The Big Bang Theory - Second Season',
'The Big Bang Theory - First Season',
'The Big Bang Theory - First Season (2007)',
'The Big Bang Theory - Fourth Season (2010)',
'The Big Bang Theory - Second Season (2008)',
'The Big Bang Theory - Third Season (2009)',
'The Big Bang Theory: Access All Areas (2012)',
'The Big Bang Theory: It All Started with a Big Bang (2012)'];

movies = [...new Set(movies.map(movie => movie.replace(/\s*\(\d+\)\s*$/g, '')))];

console.log(movies);