Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Reactjs中两个数字之间的百分比_Javascript - Fatal编程技术网

Javascript Reactjs中两个数字之间的百分比

Javascript Reactjs中两个数字之间的百分比,javascript,Javascript,我需要通过比较两个数组的相似性值来找出它们之间的百分比。例如,我有两个数组 const arrOne = ["one", "two", "three", "four", "five"]; const arrTwo = ["one", "three", "four"]; let percentage = 100 * Math.abs( (arrOne.length - arrTwo.length) / ((arrOne.length + arr

我需要通过比较两个数组的相似性值来找出它们之间的百分比。例如,我有两个数组

const arrOne = ["one", "two", "three", "four", "five"];
const arrTwo = ["one", "three", "four"];

let percentage = 100 * Math.abs(
            (arrOne.length - arrTwo.length) /
              ((arrOne.length + arrTwo.length) / 2)
          )
我需要比较这两个数组,并需要根据相似的字符串找到其中的百分比。上面的方法是我尝试过的,但没有得到预期的输出

const arrTwoInArrOneRatio = arrTwo.filter(i=>arrOne.includes(i)).length / arrOne.length
const arrOneInArrTwoRatio = arrOne.filter(i=>arrTwo.includes(i)).length / arrTwo.length

以下是两个阵列之间的重叠比率:

function intersectionRatio(arrOne, arrTwo){
    intersection = arrOne.filter(el => arrTwo.includes(el))
    elementsCount = arrOne.length + arrTwo.length - intersection.length
    return intersection.length / elementsCount
}

intersectionRatio([1,2,3], [4,5,6]); // 0
intersectionRatio([1,2,3], [1,2,3]); // 1
intersectionRatio([1,2,3], [2,3,4]); // 0.5

你会怎么“用手”做?您可能会看到一个数组中有多少包含在另一个数组中。就“百分比”而言,不清楚你的意思是什么,因为问题中没有提供标准。你可以用它。这段代码对大小有一定的影响——它不考虑任何内容。我不知道您期望的输出是什么(因为您没有说),但您自己的问题陈述清楚地表明,纯粹基于大小做一些事情是没有意义的。也许先看看实际的问题。我想通过比较这两个数组来得到percetage作为输出。如果两个数组都有三个字符串。在这种情况下,如果两个数组中的一个字符串相同。然后,我预计DaveNewton的产出为33.33%