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
Merge JSON array date based JavaScript
When working with JSON arrays containing objects with date properties, you often need to merge objects that share the same date. This is common when combining data from different sources or consolidating time-series data.
Suppose we have the following array of objects:
const arr = [
{
"date": "2010-01-01",
"price": 30
},
{
"date": "2010-02-01",
"price": 40
},
{
"date": "2010-03-01",
"price": 50
},
{
"date": "2010-01-01",
"price2": 45
},
{
"date": "2010-05-01",
"price2": 40
},
{
"date": "2010-10-01",
"price2": 50
}
];
console.log("Original array:");
console.log(JSON.stringify(arr, null, 2));
Original array:
[
{
"date": "2010-01-01",
"price": 30
},
{
"date": "2010-02-01",
"price": 40
},
{
"date": "2010-03-01",
"price": 50
},
{
"date": "2010-01-01",
"price2": 45
},
{
"date": "2010-05-01",
"price2": 40
},
{
"date": "2010-10-01",
"price2": 50
}
]
We need to write a JavaScript function that merges objects based on their "date" property, combining all properties from objects with matching dates.
Using reduce() Method
The most efficient approach uses the reduce() method to build a merged result:
const arr = [
{ "date": "2010-01-01", "price": 30 },
{ "date": "2010-02-01", "price": 40 },
{ "date": "2010-03-01", "price": 50 },
{ "date": "2010-01-01", "price2": 45 },
{ "date": "2010-05-01", "price2": 40 },
{ "date": "2010-10-01", "price2": 50 }
];
const mergeByDate = (arr) => {
const merged = arr.reduce((acc, obj) => {
const existingItem = acc.find(item => item.date === obj.date);
if (existingItem) {
// Merge properties into existing object
Object.assign(existingItem, obj);
} else {
// Add new object to accumulator
acc.push({ ...obj });
}
return acc;
}, []);
// Sort by date
return merged.sort((a, b) => new Date(a.date) - new Date(b.date));
};
const result = mergeByDate(arr);
console.log("Merged array:");
console.log(JSON.stringify(result, null, 2));
Merged array:
[
{
"date": "2010-01-01",
"price": 30,
"price2": 45
},
{
"date": "2010-02-01",
"price": 40
},
{
"date": "2010-03-01",
"price": 50
},
{
"date": "2010-05-01",
"price2": 40
},
{
"date": "2010-10-01",
"price2": 50
}
]
Using Map for Better Performance
For larger datasets, using a Map provides better performance:
const mergeByDateMap = (arr) => {
const dateMap = new Map();
arr.forEach(obj => {
if (dateMap.has(obj.date)) {
// Merge with existing object
Object.assign(dateMap.get(obj.date), obj);
} else {
// Create new entry
dateMap.set(obj.date, { ...obj });
}
});
// Convert Map values to array and sort
return Array.from(dateMap.values()).sort((a, b) => new Date(a.date) - new Date(b.date));
};
const result2 = mergeByDateMap(arr);
console.log("Merged using Map:");
console.log(JSON.stringify(result2, null, 2));
Merged using Map:
[
{
"date": "2010-01-01",
"price": 30,
"price2": 45
},
{
"date": "2010-02-01",
"price": 40
},
{
"date": "2010-03-01",
"price": 50
},
{
"date": "2010-05-01",
"price2": 40
},
{
"date": "2010-10-01",
"price2": 50
}
]
Comparison
| Method | Time Complexity | Best For |
|---|---|---|
| reduce() + find() | O(n²) | Small arrays |
| Map-based | O(n) | Large datasets |
Key Points
- Objects with the same date are merged into a single object
- Properties from multiple objects are combined using
Object.assign() - The final array is sorted chronologically by date
- Map-based approach is more efficient for large datasets
Conclusion
Merging JSON arrays by date is essential for data consolidation. Use the reduce method for simplicity or Map for better performance with large datasets. Both approaches preserve all properties while eliminating date duplicates.
