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

时间:2016-11-13 22:12:13

标签: javascript arrays string similarity

想象一下,我有一个如下所示的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)

如何省略类似的项目?你有什么解决方案?

由于

1 个答案:

答案 0 :(得分:3)

您可以从每个标题中删除括号中的位,将其转储到Set中 - 这会消除重复项,并将其转回数组:

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);

相关问题