简体   繁体   English

如何使用 JavaScript 语言比较两个对象并返回一个介于 0 和 100 之间的数字,表示相似度的百分比作为结果

[英]How to compare two objects using JavaScript language and return a number between 0 and 100 representing the percentage of similarity as the result

I followed this tutorial ( https://www.tutorialspoint.com/compare-two-objects-in-javascript-and-return-a-number-between-0-and-100-representing-the-percentage-of-similarity ) to be able to compare two objects in JavaScript and return a number between 0 and 100 representing the percentage of similarity.我按照本教程( https://www.tutorialspoint.com/compare-two-objects-in-javascript-and-return-a-number-between-0-and-100-representing-the-percentage-of-similarity ) 以便能够在 JavaScript 中比较两个对象并返回一个介于 0 和 100 之间的数字,表示相似度的百分比。

However, that only works for exact key-value pairs in both objects.但是,这只适用于两个对象中的精确键值对。 I also need to be able to compare number intervals (like on the third "if" statement below for example).我还需要能够比较数字间隔(例如下面的第三个“if”语句)。 The expected result is 70, but currently I am getting 80 (if you comment all "if" statements after the second one, it will give you 40 as the result, which is Correct).预期的结果是 70,但目前我得到了 80(如果您在第二个之后评论所有“if”语句,它会给您 40 作为结果,这是正确的)。

My problem starts when I write the third "if" statement and continue with the others.当我写第三个“if”语句并继续其他语句时,我的问题就开始了。 Actually, after the 3rd "if" statement, the other "if's" don't make any difference on the final result (I don't know why).实际上,在第三个“if”语句之后,其他“if”对最终结果没有任何影响(我不知道为什么)。 Could somebody help to find a suitable solution, please?有人可以帮忙找到合适的解决方案吗?

const a = {
  especie: 'cachorro',
  idade_min: 1,
  idade_max: 3,
  sexo: 'M',
  peso_min: 5,
  peso_max: 10,
  tamanho: 50,
  porte: 100,
  cor: 'preta',
  raça: 'pitbull',
  castrado: true,
  vacinado: true,
};

const b = {
  especie: 'cachorro',
  idade: 2,
  sexo: 'M',
  peso: 10,
  tamanho: 25,
  porte: 100,
  cor: 'branca',
  raça: 'pitbull',
  castrado: false,
  vacinado: false,
};

const findMatch = (first, second) => {
  const firstLength = Object.keys(first).length;
  const secondLength = Object.keys(second).length;
  const smaller = firstLength < secondLength ? first : second;
  const greater = smaller === first ? second : first;

  const count = Object.keys(smaller).reduce((acc, key) => {
    let counter = acc;

    if (Object.keys(greater).includes(key)) {
      if (greater[key] === smaller[key]) {
        console.log(counter);
        return ++counter;
      };

      if (b.idade <= a.idade_max && b.idade >= a.idade_min) {
        return ++counter;
      };

      if (b.peso <= a.peso_max && b.peso >= a.peso_min) {
        return ++counter;
      };

      if (b.tamanho <= a.tamanho) {
        return ++counter;
      };

      if (b.porte <= a.porte) {
        return ++counter;
      };
    };

    console.log(counter);
    return counter;
  }, 0);

  return (count / Math.min(firstLength, secondLength)) * 100;
};

console.log(findMatch(a, b)); // expected result = 70?

your function isn't dynamic , it use constant object values and keys from global scope anyway check this it will work fine i think您的函数不是动态的,它使用全局范围内的常量对象值和键无论如何检查它会正常工作我认为

 const a = { especie: 'cachorro', idade_min: 1, idade_max: 3, sexo: 'M', peso_min: 5, peso_max: 10, tamanho: 50, porte: 100, cor: 'preta', raça: 'pitbull', castrado: true, vacinado: true, }; const b = { especie: 'cachorro', idade: 2, sexo: 'M', peso: 10, tamanho: 25, porte: 100, cor: 'branca', raça: 'pitbull', castrado: false, vacinado: false, }; const findMatch = (first, second) => { const firstLength = Object.keys(first).length; const secondLength = Object.keys(second).length; const smaller = firstLength < secondLength ? first : second; const greater = smaller === first ? second : first; const smallObjKeys = Object.keys(smaller) const count = smallObjKeys.reduce((counter, key) => { // this to check the similarity between normal keys if (greater[key] === smaller[key]) return ++counter; // this to check the similarity between keys have range to compare with if (smaller[key] <= greater[`${key}_max`] && smaller[key] >= greater[`${key}_min`]) return ++counter // just to show you the different key console.log(`the ${key} is not the same`); return counter; }, 0); return (count / Math.min(firstLength, secondLength)) * 100; }; console.log(findMatch(a, b)); // expected result = 70? // WRONG ^^ it's 60%

and i think you are wrong about the expected output good luck我认为你对预期的输出是错误的祝你好运

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

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