
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 Articles
- Compare Strings in JavaScript and return percentage of likeliness
- How to return a random number between 0 and 199 with JavaScript?
- Compare two arrays of single characters and return the difference? JavaScript
- How to compare two objects in JavaScript?
- Representing number as the power and product of primes in JavaScript
- How to compare two JavaScript Date Objects?
- Compare two tables and return missing ids in MySQL?
- Compare array of objects - JavaScript
- How to compare two JavaScript array objects using jQuery/JavaScript?
- Compare two arrays and return the element-wise minimum in Numpy
- Compare two arrays and return the element-wise maximum in Numpy
- JavaScript Compare two sentences word by word and return if they are substring of each other
- Return a map representing the frequency of each data type in an array in JavaScript
- Compare return inward and return outward
- Compare two objects of Character type in Java
