Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

96
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda