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
Any way to solve this without concatenating these two arrays to get objects with higher value?
To find objects with higher property values from two arrays without concatenation, use the reduce() method on both arrays individually. This approach compares objects by a specific property and keeps only those with the highest values.
Problem Setup
Consider two arrays containing student objects with names and marks from different sections:
var sectionAStudentDetails = [
{studentName: 'John', studentMarks: 78},
{studentName: 'David', studentMarks: 65},
{studentName: 'Bob', studentMarks: 98}
];
let sectionBStudentDetails = [
{studentName: 'John', studentMarks: 67},
{studentName: 'David', studentMarks: 89},
{studentName: 'Bob', studentMarks: 97}
];
console.log("Section A:", sectionAStudentDetails);
console.log("Section B:", sectionBStudentDetails);
Section A: [
{ studentName: 'John', studentMarks: 78 },
{ studentName: 'David', studentMarks: 65 },
{ studentName: 'Bob', studentMarks: 98 }
]
Section B: [
{ studentName: 'John', studentMarks: 67 },
{ studentName: 'David', studentMarks: 89 },
{ studentName: 'Bob', studentMarks: 97 }
]
Solution Using reduce()
The key is to use a reducer function that compares objects by their property values and keeps the one with the higher value:
var sectionAStudentDetails = [
{studentName: 'John', studentMarks: 78},
{studentName: 'David', studentMarks: 65},
{studentName: 'Bob', studentMarks: 98}
];
let sectionBStudentDetails = [
{studentName: 'John', studentMarks: 67},
{studentName: 'David', studentMarks: 89},
{studentName: 'Bob', studentMarks: 97}
];
function getHigherValueObjects(accumulator, currentStudent) {
const existingStudent = accumulator[currentStudent.studentName];
if (!existingStudent || currentStudent.studentMarks >= existingStudent.studentMarks) {
accumulator[currentStudent.studentName] = currentStudent;
}
return accumulator;
}
// First, process section A
const sectionA = sectionAStudentDetails.reduce(getHigherValueObjects, {});
// Then process section B, using section A as initial value
const finalResult = sectionBStudentDetails.reduce(getHigherValueObjects, sectionA);
console.log("Students with highest marks:");
console.log(Object.values(finalResult));
Students with highest marks:
[
{ studentName: 'John', studentMarks: 78 },
{ studentName: 'David', studentMarks: 89 },
{ studentName: 'Bob', studentMarks: 98 }
]
How It Works
The reducer function works as follows:
- Uses the student's name as a key in the accumulator object
- Compares the current student's marks with existing marks for the same name
- Keeps the student object with higher marks
- Returns the updated accumulator
Alternative Approach
You can also combine both arrays first and then apply the reduction:
var sectionAStudentDetails = [
{studentName: 'John', studentMarks: 78},
{studentName: 'David', studentMarks: 65},
{studentName: 'Bob', studentMarks: 98}
];
let sectionBStudentDetails = [
{studentName: 'John', studentMarks: 67},
{studentName: 'David', studentMarks: 89},
{studentName: 'Bob', studentMarks: 97}
];
// Combine both arrays and apply reduce once
const allStudents = [...sectionAStudentDetails, ...sectionBStudentDetails];
const result = allStudents.reduce((acc, student) => {
const existing = acc[student.studentName];
if (!existing || student.studentMarks > existing.studentMarks) {
acc[student.studentName] = student;
}
return acc;
}, {});
console.log("Result using spread operator:");
console.log(Object.values(result));
Result using spread operator:
[
{ studentName: 'John', studentMarks: 78 },
{ studentName: 'David', studentMarks: 89 },
{ studentName: 'Bob', studentMarks: 98 }
]
Conclusion
Using reduce() with a custom comparison function effectively finds objects with higher property values without concatenating arrays. The reducer function maintains an object map using unique identifiers as keys, ensuring only the highest-value objects are retained.
