Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
Cómo comparar dos objetos utilizando el lenguaje JavaScript y devolver un número entre 0 y 100 que representa el porcentaje de similitud como resultado

Seguí este tutorial ( https://www.tutorialspoint.com/compare-two-objects-in-javascript-and-return-a-number- between-0-and-100-representing-the-percentage-of-similarity ) para poder comparar dos objetos en JavaScript y devolver un número entre 0 y 100 que representa el porcentaje de similitud.

Sin embargo, eso solo funciona para pares clave-valor exactos en ambos objetos. También necesito poder comparar intervalos numéricos (como en la tercera declaración "si" a continuación, por ejemplo). El resultado esperado es 70, pero actualmente obtengo 80 (si comenta todas las declaraciones "si" después de la segunda, obtendrá 40 como resultado, que es correcto).

Mi problema comienza cuando escribo la tercera declaración "si" y continúo con las demás. En realidad, después de la tercera declaración "si", los otros "si" no hacen ninguna diferencia en el resultado final (no sé por qué). ¿Alguien podría ayudar a encontrar una solución adecuada, por favor?

 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?
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

su función no es dinámica, usa valores de objetos constantes y claves de alcance global de todos modos, compruebe que funcionará bien, creo

 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%

y creo que te equivocas sobre el resultado esperado buena suerte

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!