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

132
Views
Javascript find dictionary items whose key matches a substring in an optimized way

Consider the below scenario

const searchString = 'Gen';

const myDict = {
'Genesis': 'You are the beginning',
'Joel': 'Joe is cool'
 // Many other key value pairs
}

I need to get You are the beginning because searchString(Gen) is a substring of Genesis.

How can I achieve this in an optimized way in JS?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could do something like:

const searchString = 'Gen';

const myDict = {
'Genesis': 'You are the beginning',
'Joel': 'Joe is cool'
 // Many other key value pairs
}


for (let key in myDict){
    if(key.includes(searchString)){
        console.log(myDict[key]) // You are the beginning
    }
}
about 4 years ago · Juan Pablo Isaza Report

0

You could use find()

const searchString = 'Gen';

const myDict = {
'Genesis': 'You are the beginning',
'Joel': 'Joe is cool'
 // Many other key value pairs
}

const result = Object.entries(myDict).find(([k]) => k.includes(searchString));

console.log(result[1]);

about 4 years ago · Juan Pablo Isaza Report

0

const searchString = 'Gen';

const myDict = {
'Genesis': 'You are the beginning',
'Joel': 'Joe is cool'
 // Many other key value pairs
}

const result = Object.entries(myDict).find(([k]) => k.includes(searchString));

console.log(result[1]);

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!