简体   繁体   English

JavaScript数组字符串比较

[英]JavaScript array string compare

I created one function with JavaScript which compare two string and return the number of the same characters with the following logic: 我使用JavaScript创建了一个函数,该函数比较两个字符串并使用以下逻辑返回相同字符的数量:

Char 1 = “aaabc” | 字符1 =“ aaabc” | Char 2 = “aakbc” ===> My function return 2 字符2 =“ aakbc” ===>我的函数返回2

Char 2 = “88835” | 字符2 =“ 88835” | Char 2 = “888vbr” ===> My function return 3 字符2 =“ 888vbr” ===>我的函数返回3

Char 1 = “A1234” | 字符1 =“ A1234” | Char 2 = “B1234” ===> My function return 0 字符2 =“ B1234” ===>我的函数返回0

The logic is that when the function find that the FIRST character of CHAR1 is DIFFERENT is NOT EQUAL than the FIRST character of CHAR2 the function stop the iteration and return 0, if not: the function continue until we find that the CHAR1(i) !== CHAR2(i). 逻辑是,当函数发现CHAR1的FIRST字符与CHAR2的FIRST字符不相等时,函数将停止迭代并返回0(如果不是):该函数继续直到我们发现CHAR1(i)为止! == CHAR2(i)。

I am using this function to compare between two array of string, T[i] and V[j]. 我正在使用此函数在两个字符串数组T [i]和V [j]之间进行比较。 For each value of T[i] I am browsing all V[j] and returning the row which is more similar that T[i] and if the function find same result, I will return the smallest value V[j]. 对于T [i]的每个值,我浏览所有V [j]并返回与T [i]更相似的行,如果函数找到相同的结果,我将返回最小值V [j]。 This is the code that I used: 这是我使用的代码:

 function MyFunction(a, b) { var n = a.length, m = b.length; var v; var i = 1; var j = 1; if (a === b) { v = a.length; } else if (a.charCodeAt(0) !== b.charCodeAt(0)) { v = 0; } else { v = 1; for (i = 1; i < n; i++) { if (a.charCodeAt(i) == b.charCodeAt(i)) { v++; } else { return v; } } } return v; } var t = ["350", "840", "35"], v = ["3506", "35077", "84"], i, j, f, l, max, result = [], row = []; for (i = 0; i < t.length; i++) { max = MyFunction(v[0], t[i]); l = v[0].length; f = [ [t[0]], [v[0]] ]; for (j = 1; j < v.length; j++) { if (MyFunction(v[j], t[i]) > max) { max = MyFunction(v[j], t[i]); f = [ [t[i]], [v[j]] ]; l = v[j].length; } else { if (MyFunction(v[j], t[i]) == max && l > v[j].length) { max = MyFunction(v[j], t[i]); f = [ [t[i]], [v[j]] ]; l = v[j].length; } else { continue; } } } result.push(f); console.log(f); } 

My code have an issue, the result which I have is: 我的代码有问题,我得到的结果是:

[350][3506] (Correct value) [350] [3506](正确值)

[840][84] (Correct value) [840] [84](正确值)

[350][3506] (Wrong value) [350] [3506](值错误)

I don't find a solution for this issue, my code don't compare the value [35], the code is comparing the first value [350] (this is the issue). 我没有找到解决此问题的方法,我的代码未比较值[35],代码正在比较第一个值[350](这是问题)。

In each loop of the outer for you start with initialize max to MyFunction(v[0], t[i]) , and then scan-compare all the elements in the array. 在外部的每个环路for你开始初始化maxMyFunction(v[0], t[i])然后将阵列中的所有元件扫描进行比较。 but, in the 3rd case, the resault of this check larger than al the other, so all the comparison in this loop get false and the final resault is what you see. 但是,在第3种情况下,此检查的重发大于其他检查,因此此循环中的所有比较都为false ,最终的重发就是您所看到的。

you can solve it if you initialize max = 0 and then loop over all indices from 0 (include). 如果您初始化max = 0 ,然后从0(包括)循环遍历所有索引,则可以解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM