Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!