我需要通过比较它们的相似度值来找出两个数组之间的百分比。例如,我有两个数组。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) )我需要比较这两个数组,并需要根据相似的字符串找到它的百分比。上面的方法是,我尝试过,但没有得到预期的输出
2 回答
德玛西亚99
TA贡献1770条经验 获得超3个赞
这是两个数组之间的重叠率:
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
收到一只叮咚
TA贡献1821条经验 获得超4个赞
试试这个:
const arrTwoInArrOneRatio = arrTwo.filter(i=>arrOne.includes(i)).length / arrOne.length
const arrOneInArrTwoRatio = arrOne.filter(i=>arrTwo.includes(i)).length / arrTwo.length
添加回答
举报
0/150
提交
取消