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

217
Visualizações
How to compare 2 arrays of objects with duplicate elements

I want to compare 2 array of objects with floating point property to see if they are equal, regardless of the order of elements. The array may contain duplicate elements. I already have a function to determine whether two objects are equal, however, I have no idea how to compare 2 arrays.

const arr1 = [
  {
    foo: "obj1",
    bar: 0.1
  },
  {
    foo: "obj2",
    bar: 0.3
  },
  {
    foo: "obj2",
    bar: 0.3
  }
];
const arr2 = [
  {
    foo: "obj2",
    bar: 0.1 + 0.2
  },
  {
    foo: "obj1",
    bar: 0.1
  },
  {
    foo: "obj2",
    bar: 0.3
  }
];

const compareObject = (o1, o2) => {
  if (o1.foo !== o2.foo) {
    return false;
  }
  const threshold = 0.00000001;
  return Math.abs(o1.bar - o2.bar) < threshold;
};
const compareArr = (arr1, arr2) => {
  // how to compare?
};

compareArr(arr1, arr2) // should return true

Clarify: The function should only return true if 2 arrays are exactly matched regardless of the order of elements, and there may be duplicate elements inside an array, i.e.

const arr1 = [obj1, obj1, obj2];
const arr2 = [obj1, obj2, obj2];

compareArr(arr1, arr2) // should return false
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

const arr1 = [
  {
    foo: "obj1",
    bar: 0.1
  },
  {
    foo: "obj2",
    bar: 0.3
  },
  {
    foo: "obj2",
    bar: 0.3
  }
];
const arr2 = [
  {
    foo: "obj2",
    bar: 0.1 + 0.2
  },
  {
    foo: "obj1",
    bar: 0.1
  },
  {
    foo: "obj2",
    bar: 0.3
  }
];

const arr3 = [
  {
    foo: "obj2",
    bar: 0.1 + 0.1
  },
  {
    foo: "obj1",
    bar: 0.1
  },
  {
    foo: "obj2",
    bar: 0.3
  }
];
function sortObject( a, b ) {
  if ( a.foo < b.foo ){
    return -1;
  }
  if ( a.foo > b.foo ){
    return 1;
  }
  
  if ( a.bar < b.bar ){
    return -1;
  }
  if ( a.bar > b.bar ){
    return 1;
  }
}

const compareObject = (o1, o2) => {
  if (o1.foo !== o2.foo) {
    return false;
  }
  const threshold = 0.00000001;
  return Math.abs(o1.bar - o2.bar) < threshold;
};

arr1.sort(sortObject);
arr2.sort(sortObject);
arr3.sort(sortObject);

function compare (arr1, arr2) {
  let result = true;
  if (arr1.length != arr2.length) return false;
  arr1.every(function(element, index) {
    // Do your thing, then:
    if (!compareObject(element, arr2[index])) {
      result = false;
      return false;
    }
    return true;
  });
  return result;
}

console.log(compare(arr1, arr2));
console.log(compare(arr1, arr3));

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use the every function

The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

Example :

const arr1 = [
  {
    foo: "obj1",
    bar: 0.1
  },
  {
    foo: "obj2",
    bar: 0.3
  },
  {
    foo: "obj2",
    bar: 0.3
  }
];
const arr2 = [
  {
    foo: "obj2",
    bar: 0.1 + 0.2
  },
  {
    foo: "obj1",
    bar: 0.1
  },
  {
    foo: "obj2",
    bar: 0.3
  }
];

const compareObject = (o1, o2) => {
  if (o1.foo !== o2.foo) {
    return false;
  }
  const threshold = 0.00000001;
  return Math.abs(o1.bar - o2.bar) < threshold;
};
const compareArr = (arr1, arr2) => {
  return arr1.every(arr1Item => arr2.find(arr2Item => compareObject(arr1Item, arr2Item)))
};

console.log(compareArr(arr1, arr2)) // should return true

You can also use the some function

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

And check if one element does not pass the test.

const arr1 = [
  {
    foo: "obj1",
    bar: 0.1
  },
  {
    foo: "obj2",
    bar: 0.3
  },
  {
    foo: "obj2",
    bar: 0.3
  }
];
const arr2 = [
  {
    foo: "obj2",
    bar: 0.1 + 0.2
  },
  {
    foo: "obj1",
    bar: 0.1
  },
  {
    foo: "obj2",
    bar: 0.3
  }
];

const compareObject = (o1, o2) => {
  if (o1.foo !== o2.foo) {
    return false;
  }
  const threshold = 0.00000001;
  return Math.abs(o1.bar - o2.bar) < threshold;
};
const compareArr = (arr1, arr2) => {
  return !arr1.some(arr1Item => !arr2.find(arr2Item => compareObject(arr1Item, arr2Item)))
};

console.log(compareArr(arr1, arr2)) // should return true

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