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

224
Visualizações
Check if an array includes some objects

As shown below, if I would like to find whether an object in testArray is in the currentArray, I can use for loop to traverse arrays, or I can use find(), some(), include(). The worst performance is if I go all fall into at least once O(n^2).

If I firstly filter the items in currentArray which the balance is not 999 or 999.999 and then to check testArray. Does the performance be better than fall into at least once? Or is there any way that doing better?

Thanks!

let currentArray = [
  {account: 1, balance: 200},
  {account: 2, balance: 100},
  {account: 3, balance: 300},
  {account: 4, balance: 999},
  {account: 5, balance: 999.999},
  {account: 6, balance: 999},
  {account: 15, balance: 999.999},
  {account: 8, balance: 100},
  {account: 9, balance: 300},
  {account: 10, balance: 300},
]

let testArray = [
  {account: 4, balance: 999},
  {account: 5, balance: 999.999},
  {account: 6, balance: 999},
  {account: 7, balance: 999.999},
  {account: 8, balance: 999},
  {account: 9, balance: 999.999},
  {account: 10, balance: 999},
  {account: 11, balance: 999.999},
  {account: 12, balance: 999},
  {account: 13, balance: 999.999},
  {account: 14, balance: 999},
  {account: 15, balance: 999.999},
]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

From what I understand, you expect the following result for the given example arrays:

[
    { "account":  4, "balance": 999     },
    { "account":  5, "balance": 999.999 },
    { "account":  6, "balance": 999     },
    { "account": 15, "balance": 999.999 }
]

...as these occur in both arrays.

To achieve that with a better time complexity, use a unique key for each object. Knowing that your objects have a key that consists of two properties, you can combine those two values in an array and produce its JSON encoding (or any other suitable encoding): that string will be a unique id for each object. Store those of the first array in a set, and then iterate the second array to check if the object has such an identifier that occurs in the set.

let currentArray = [
  {account: 1, balance: 200},
  {account: 2, balance: 100},
  {account: 3, balance: 300},
  {account: 4, balance: 999},
  {account: 5, balance: 999.999},
  {account: 6, balance: 999},
  {account: 15, balance: 999.999},
  {account: 8, balance: 100},
  {account: 9, balance: 300},
  {account: 10, balance: 300},
];

let testArray = [
  {account: 4, balance: 999},
  {account: 5, balance: 999.999},
  {account: 6, balance: 999},
  {account: 7, balance: 999.999},
  {account: 8, balance: 999},
  {account: 9, balance: 999.999},
  {account: 10, balance: 999},
  {account: 11, balance: 999.999},
  {account: 12, balance: 999},
  {account: 13, balance: 999.999},
  {account: 14, balance: 999},
  {account: 15, balance: 999.999},
];

const hash = ({account, balance}) => JSON.stringify([account, balance]);

let ref = new Set(currentArray.map(hash));
let result = testArray.filter(obj => ref.has(hash(obj)));
console.log(result);

If you are only interested whether there is such an object, then use some instead of filter. The return value will be a boolean: true when there is such an object, and false otherwise.

If you are interested to find one such object, then use find instead of filter. The return value will be the first found object, or undefined when there is no such object.

The key to the better time complexity is the use of the Set. It could also be a plain object with the JSON strings as property names, and for each an arbitrary value (like 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