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

129
Visualizações
check if value exists in an Object containing multiple Objects

So I have an Object that contains multiple Objects

const edges = {
         edge1: {source: "Horse", target: "4_2_0_0_0f"},
         edge2: {source: "Horse", target: "4_2_0_0_0x"},
         edge3: {source: "Horse", target: "4_2_0_2_1h"},
         edge4: {source: "Horse", target: "4_2_4_0_4z"},
         edge5: {source: "Horse", target: "4_2_1_0_0p"},
....}

Now I want to check whether edge2: {source: "Horse", target: "4_2_0_0_0x"} exists in edges or not. What is the fastest way to do that?

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

0

You can Array.some to check if at least one of array elements matches your condition. Before that you will need to convert object you array with Object.values:

const edges = {
  edge1: {
    source: "Horse",
    target: "4_2_0_0_0f"
  },
  edge2: {
    source: "Horse",
    target: "4_2_0_0_0x"
  },
  edge3: {
    source: "Horse",
    target: "4_2_0_2_1h"
  },
  edge4: {
    source: "Horse",
    target: "4_2_4_0_4z"
  },
  edge5: {
    source: "Horse",
    target: "4_2_1_0_0p"
  },
}

function existsEdge(source, target) {
  return Object.values(edges).some(v => v.source === source && v.target === target)
}

console.log(existsEdge('Horse', '4_2_0_0_0x'))

This is effective if you wish to check only once. If you wish to check multiple times, consider using different data structure with fasters checks. Example using Set:

const edges = {
  edge1: {
    source: "Horse",
    target: "4_2_0_0_0f"
  },
  edge2: {
    source: "Horse",
    target: "4_2_0_0_0x"
  },
  edge3: {
    source: "Horse",
    target: "4_2_0_2_1h"
  },
  edge4: {
    source: "Horse",
    target: "4_2_4_0_4z"
  },
  edge5: {
    source: "Horse",
    target: "4_2_1_0_0p"
  },
}

const index = new Set(Object.values(edges).map(v => `${v.source}:${v.target}`))

function existsEdge(source, target) {
  return index.has(`${source}:${target}`)
}

let result = undefined
for (let i = 0; i < 10000000; i++) {
  result = existsEdge('Horse', '4_2_1_0_0p')
}

console.log(result)

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