Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

103
Vistas
Comparing two objects that contain arrays, disregarding the order of elements inside array

I have two objects with multiple properties and with multiple layers. At different layers, I might have some arrays. I want to be able to compare the two objects, disregarding the ordering of elements in each array.

Example 1: object1 and object2 should be "equal"

object1 = {
    person: "John",
    children: [
        {
            person: "Anne"
        },
        {
            person: "Mark"
        }
    ]
};

object2 = {
    person: "John",
    children: [
        {
            person: "Mark"
        },
        {
            person: "Anne"
        }
    ]
};

// This would have to return true:
expect(object1).to."fundamentally".equal(object2);

Example2: a bit more complicated, object1 and object2 should still be the same

object1 = {
    person: "John",
    children: [
        {
            person: "Anne"
        },
        {
            person: "Mark",
            pets: [
                {
                    species: "cat",
                    name: "Purr"
                },
                {
                    type: "labrador",
                    name: "Woofy",
                    age: 3
                }

            ]
        }
    ],
    cars: [
        {
            brand: "BMW",
            year: 2017
        },
        {
            brand: "Mercedes-Benz",
            year: 2020
        }

    ]
};

object2 = {
    person: "John",
    children: [
        {
            person: "Mark",
            pets: [
                {
                    type: "labrador",
                    name: "Woofy",
                    age: 3
                },
                {
                    species: "cat",
                    name: "Purr"
                }

            ]
        },
        {
            person: "Anne"
        }
    ],
    cars: [
        {
            brand: "Mercedes-Benz",
            year: 2020
        },
        {
            brand: "BMW",
            year: 2017
        }

    ]
};



// This would have to return true:
expect(object1).to."fundamentally".equal(object2);

Even though the children, cars and pets are in different order, I still want to recognize that both objects have the same contents.

Is there a function that would do this?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda