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

146
Visualizações
Compare Arrays javascript with different values

I am trying to compare 2 arrays where first array response is following:

response= [
      {    
        organization: {
          id: '75f1e2e1-83eb-484e-bdaf-1da9b57c635d'   
      },
      {    
        organization: {
          id: '810d0465-2917-4d67-802a-69cda41647ad'
        
      }
    ]

second array

expectedResponse=  

 [
          {      
            organization: {
              id: '810d0465-2917-4d67-802a-69cda41647ad'
            
          },
            organization: {
              id: '75f1e2e1-83eb-484e-bdaf-1da9b57c635d'   
          }  
        ]

The function I wrote to compare 2 arrays for assertion is following

response.body.forEach(function (index) {      
       expect(response.body[index].organization.id).to.eql(
        expectedResponse[index].organization.id
       );

But it is failing with AssertionError: expected '75f1e2e1-83eb-484e-bdaf-1da9b57c635d' to deeply equal '810d0465-2917-4d67-802a-69cda41647ad'

As the order is different in each arrays for org id's. How can i compare if the order is different to make this assertion pass

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As you've said, since the order is different, you can't do this with a single pass.

You have at least a couple of options, each of which you'd do after checking that the arrays have the same length (since if they don't, they aren't a match for one another):

  1. Sort the arrays (copying them first if sorting the originals isn't okay) and then use what you have to compare the results, since then they'll be in the same order.

  2. Look for matches in an order-independent fashion, such as:

    • Loop through array A using findIndex or find to see that array B has each of its values
    • Building a Set of the id values from array A, and then looping through array B making sure they're all in the set

(Again, in both cases you start by ensuring they have the same length.)

You'll probably also want to ensure that the id values are unique.

about 4 years ago · Juan Pablo Isaza Relatório
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