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 object properties through unique field then print data - JavaScript
Let's say we have a students object containing two properties names and marks. The names is an array of objects with each object having two properties name and roll, similarly marks is an array of objects with each object having properties mark and roll. Our task is to combine the marks and names properties according to the appropriate roll property of each object.
Problem Statement
The students object is given here:
const students = {
marks: [{
roll: 123,
mark: 89
}, {
roll: 143,
mark: 69
}, {
roll: 126,
mark: 91
}, {
roll: 112,
mark: 80
}],
names: [{
name: 'Aashish',
roll: 126
}, {
name: 'Sourav',
roll: 112
}, {
name: 'Vineet',
roll: 143
}, {
name: 'Kartik',
roll: 123
}]
}
console.log("Original students object:");
console.log(students);
Original students object:
{
marks: [
{ roll: 123, mark: 89 },
{ roll: 143, mark: 69 },
{ roll: 126, mark: 91 },
{ roll: 112, mark: 80 }
],
names: [
{ name: 'Aashish', roll: 126 },
{ name: 'Sourav', roll: 112 },
{ name: 'Vineet', roll: 143 },
{ name: 'Kartik', roll: 123 }
]
}
Solution: In-Place Merging
Let's define a function combineProperties that takes in the students object and combines the properties in place (without using any extra space):
const students = {
marks: [{
roll: 123,
mark: 89
}, {
roll: 143,
mark: 69
}, {
roll: 126,
mark: 91
}, {
roll: 112,
mark: 80
}],
names: [{
name: 'Aashish',
roll: 126
}, {
name: 'Sourav',
roll: 112
}, {
name: 'Vineet',
roll: 143
}, {
name: 'Kartik',
roll: 123
}]
}
const combineProperties = (students) => {
const { marks, names } = students;
marks.forEach(marksObj => {
const { roll } = marksObj;
marksObj.name = names.find(namesObj => namesObj.roll === roll).name;
});
delete students['names'];
};
combineProperties(students);
console.log("After combining properties:");
console.log(students);
After combining properties:
{
marks: [
{ roll: 123, mark: 89, name: 'Kartik' },
{ roll: 143, mark: 69, name: 'Vineet' },
{ roll: 126, mark: 91, name: 'Aashish' },
{ roll: 112, mark: 80, name: 'Sourav' }
]
}
How It Works
The solution works by:
- Destructuring: Extract marks and names arrays from the students object
- Iteration: Loop through each object in the marks array using forEach
- Matching: For each marks object, find the corresponding name object with the same roll number using find()
- Merging: Add the name property to each marks object
- Cleanup: Remove the names array to save memory
Alternative Approach: Creating New Array
If you prefer not to modify the original object, here's an approach that creates a new merged array:
const students = {
marks: [{
roll: 123,
mark: 89
}, {
roll: 143,
mark: 69
}, {
roll: 126,
mark: 91
}, {
roll: 112,
mark: 80
}],
names: [{
name: 'Aashish',
roll: 126
}, {
name: 'Sourav',
roll: 112
}, {
name: 'Vineet',
roll: 143
}, {
name: 'Kartik',
roll: 123
}]
}
const mergeStudentData = (students) => {
const { marks, names } = students;
return marks.map(marksObj => {
const nameObj = names.find(n => n.roll === marksObj.roll);
return { ...marksObj, name: nameObj.name };
});
};
const mergedStudents = mergeStudentData(students);
console.log("Merged students array:");
console.log(mergedStudents);
Merged students array:
[
{ roll: 123, mark: 89, name: 'Kartik' },
{ roll: 143, mark: 69, name: 'Vineet' },
{ roll: 126, mark: 91, name: 'Aashish' },
{ roll: 112, mark: 80, name: 'Sourav' }
]
Performance Analysis
The time complexity of this code is O(m×n) where m and n are the respective sizes of arrays names and marks. The space complexity is O(1) for the in-place approach, though a new property is being created for each element of marks array.
Conclusion
This approach efficiently merges object properties based on a unique field (roll number). The in-place method saves memory, while the alternative approach preserves the original data structure.
