I have a data like this:
dictionary: [
{ mercury: 'MERC' },
{ venus: 'VEN' },
{ earth: 'EART' },
{ mars: 'MAR' },
{ jupiter: 'JUP' },
{ saturn: 'SAT' },
{ uranus: 'ANUS' },
{ neptune: 'NEP' },
{ pluto: 'PLUT' },
]
When a user types letters inside an input field like v, I want to show the closest similar string from the object, which would be 'VEN' for this, I have the following code:
let word = 'v'
let result = dictionary.find(d => {
var str = Object.keys(d)[0]
if (str.toLowerCase().indexOf(word.toLowerCase()) == 0) {
return result = str
}
})
This code works well, and result returns the found string. The problem is that when someone types m I only get MERC but I should get multiple results because mercury and mar both start with m.
Is there an way to do this with find, or other function?
indexOf(word.toLowerCase()) == 0should beindexOf(word.toLowerCase()) >= 0