
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compare two objects in JavaScript and return a number between 0 and 100 representing the percentage of similarity
Let’s say, we have two objects like these −
const a = { Make: "Apple", Model: "iPad", hasScreen: "yes", Review: "Great product!", }; const b = { Make: "Apple", Model: "iPad", waterResistant: false };
We are required to write a function that counts the number of common properties in the objects (by common property we mean having both key and value same) and returns a number between 0 and 100 (both inclusive) that represents the percentage of similarity between the objects. Like if no key/value pair matches it will be 0, if all matches it will be 100.
To count the percentage of similarity we can simply divide the count of similar property by the number of properties in the smaller object (one that have lesser key/value pairs) and multiply this result by 100.
So, with that understood, now let’s write the code for this function −
Example
const a = { Make: "Apple", Model: "iPad", hasScreen: "yes", Review: "Great product!", }; const b = { Make: "Apple", Model: "iPad", waterResistant: false }; const findSimilarity = (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, val) => { if(Object.keys(greater).includes(val)){ if(greater[val] === smaller[val]){ return ++acc; }; }; return acc; }, 0); return (count / Math.min(firstLength, secondLength)) * 100; }; console.log(findSimilarity(a, b));
Output
The output in the console will be −
66.66666666666666
Because the smaller object has 3 properties of which 2 are common which amount to approximately 66%.
- Related Questions & Answers
- Compare Strings in JavaScript and return percentage of likeliness
- Compare two arrays of single characters and return the difference? JavaScript
- How to return a random number between 0 and 199 with JavaScript?
- Representing number as the power and product of primes in JavaScript
- How to compare two objects in JavaScript?
- How to compare two JavaScript Date Objects?
- Compare two tables and return missing ids in MySQL?
- Compare array of objects - JavaScript
- Compare two arrays and return the element-wise minimum in Numpy
- Compare two arrays and return the element-wise maximum in Numpy
- Compare return inward and return outward
- Compare two objects of Character type in Java
- How to get a pseudo-random number between 0 and 1 in JavaScript?
- Checking for the similarity of two 2-D arrays in JavaScript
- Get the property of the difference between two objects in JavaScript