Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Normalize numbers in an object - JavaScript
Number normalization scales values from one range to another proportionally. This technique is commonly used in data processing, machine learning, and graphics programming.
Problem Definition
Given an object with numeric values and a target range [a, b], we need to transform all values so that:
- The smallest original value becomes
a - The largest original value becomes
b - Other values are scaled proportionally between
aandb
Normalization Formula
The formula for linear normalization is:
normalized_value = range_min + ((value - original_min) * (range_max - range_min)) / (original_max - original_min)
Example Implementation
const obj = {
num1: 45,
num2: 78,
num3: 234,
num4: 3,
num5: 79,
num6: 23
};
const range = [10, 15];
const normalizeObject = (obj, range) => {
const values = Object.values(obj);
const min = Math.min(...values);
const max = Math.max(...values);
const variation = (range[1] - range[0]) / (max - min);
Object.keys(obj).forEach(key => {
const normalizedValue = range[0] + ((obj[key] - min) * variation);
obj[key] = Math.round(normalizedValue * 100) / 100; // Round to 2 decimals
});
};
console.log("Original object:", obj);
normalizeObject(obj, range);
console.log("Normalized object:", obj);
Original object: { num1: 45, num2: 78, num3: 234, num4: 3, num5: 79, num6: 23 }
Normalized object: { num1: 10.91, num2: 11.62, num3: 15, num4: 10, num5: 11.65, num6: 10.43 }
How It Works
The algorithm follows these steps:
- Extract all numeric values from the object
- Find the minimum (3) and maximum (234) values
- Calculate the scaling factor:
(15 - 10) / (234 - 3) = 5/231 ? 0.0216 - Apply the normalization formula to each value
Non-Mutating Version
To preserve the original object, create a new normalized object:
const normalizeObjectCopy = (obj, range) => {
const values = Object.values(obj);
const min = Math.min(...values);
const max = Math.max(...values);
const variation = (range[1] - range[0]) / (max - min);
const normalized = {};
Object.keys(obj).forEach(key => {
const normalizedValue = range[0] + ((obj[key] - min) * variation);
normalized[key] = Math.round(normalizedValue * 100) / 100;
});
return normalized;
};
const original = { a: 10, b: 50, c: 90 };
const normalized = normalizeObjectCopy(original, [0, 1]);
console.log("Original:", original);
console.log("Normalized:", normalized);
Original: { a: 10, b: 50, c: 90 }
Normalized: { a: 0, b: 0.5, c: 1 }
Common Use Cases
- Data visualization: Scale data points to fit chart dimensions
- Machine learning: Feature scaling for neural networks
- Image processing: Normalize pixel values to 0-255 range
- Game development: Scale scores or health values
Conclusion
Object normalization transforms numeric values proportionally to fit a target range. This technique preserves the relative relationships between values while scaling them to the desired bounds.
Advertisements
