Open In App

How to compare Arrays of Objects in JavaScript ?

In this article, we will try to understand how we may compare Arrays of Objects in JavaScript using certain techniques via some coding examples along with their theoretical explanations.

Let us first analyze the syntax by which we may declare an array of objects in JavaScript, which is shown below:



Syntax: Following syntax, we may use in order to declare an array with multiple objects:

let array_of_objects = [
    {
        property_name: value,
        ...
    },
    {
        property_name: value,
        ...
    },
    ...
]

Now after analyzing the above-shown syntax, let’s see a quick example that will help us to understand the above syntax more clearly.



Example 1: In this example, we will try to create an array with multiple objects that contains multiple properties (or keys) along with their values, and then we will try to print them in a different manner.




let employees_details = [
    {
        employee_id: 1,
        employee_name: "Aman",
        employee_age: 22,
    },
    {
        employee_id: 2,
        employee_name: "Ramesh",
        employee_age: 26,
    },
    {
        employee_id: 3,
        employee_name: "Suresh",
        employee_age: 33,
    },
];
 
console.log(
    "Complete Array of Objects is as shown below: ");
console.log(employees_details);
 
console.log(
    "\nEmployees Names and their respective ages are as follows: ");
employees_details.forEach((element) => {
    console.log(element.employee_name + " : " + element.employee_age);
});

Output:

Complete Array of Objects is as shown below: 
[
  { employee_id: 1, employee_name: 'Aman', employee_age: 22 },
  { employee_id: 2, employee_name: 'Ramesh', employee_age: 26 },
  { employee_id: 3, employee_name: 'Suresh', employee_age: 33 }
]

Employees Names and their respective ages are as follows: 
Aman : 22
Ramesh : 26
Suresh : 33

Now let us have a look over the following set of examples that will help us to understand our main task of comparing two arrays of objects.

Example 2: 




let mechanical_students_details = [
    {
        student_id: 1,
        student_name: "Ramesh",
        student_age: 22,
    },
    {
        student_id: 2,
        student_name: "Suresh",
        student_age: 26,
    },
];
 
let cse_students_details = [
    {
        student_id: 1,
        student_name: "Aman",
        student_age: 23,
    },
    {
        student_id: 2,
        student_name: "Chaitanya",
        student_age: 24,
    },
    {
        student_id: 3,
        student_name: "Dharmendra",
        student_age: 21,
    },
];
 
let compareTwoArrayOfObjects = (
    first_array_of_objects,
    second_array_of_objects
) => {
    return (
        first_array_of_objects.length === second_array_of_objects.length &&
        first_array_of_objects.every((element_1) =>
            second_array_of_objects.some(
                (element_2) =>
                    element_1.student_name === element_2.student_name &&
                    element_1.student_age === element_2.student_age
            )
        )
    );
};
 
console.log(
    "Are these passed in array of objects equal? : " +
    compareTwoArrayOfObjects(mechanical_students_details, cse_students_details)
);

Output:

Are these passed in array of objects equal? : false

Example 3: 




let mechanical_students_details = [
    {
        student_id: 1,
        student_name: "Ramesh",
        student_age: 22,
    },
    {
        student_id: 2,
        student_name: "Suresh",
        student_age: 26,
    },
];
 
let cse_students_details = [
    {
        student_id: 1,
        student_name: "Aman",
        student_age: 23,
    },
    {
        student_id: 2,
        student_name: "Chaitanya",
        student_age: 24,
    },
    {
        student_id: 3,
        student_name: "Dharmendra",
        student_age: 21,
    },
];
 
let compareTwoArrayOfObjects = (
    first_array_of_objects,
    second_array_of_objects
) => {
    return (
        first_array_of_objects.length === second_array_of_objects.length &&
        first_array_of_objects.every((element_1) =>
            second_array_of_objects.some((element_2) =>
                Object.keys(element_1).every((key) => element_1[key] === element_2[key])
            )
        )
    );
};
 
console.log(
    "Are these passed in array of objects equal? : " +
    compareTwoArrayOfObjects(mechanical_students_details, cse_students_details)
);

Output:

Are these passed in array of objects equal? : false

Article Tags :