hidar
hidar

Reputation: 5939

How to iterate through javascript object to find multiple similar keys

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?

Upvotes: 1

Views: 102

Answers (3)

gurvinder372
gurvinder372

Reputation: 68393

find returns only one value.

Use Object.values and filter

var search = "m";
search = search.toLowerCase();
var output = Object.values(dictionary).map(s => Object.values(s)[0].toLowerCase()).filter(s => s.indexOf(search) == 0);
console.log( output );

Demo

var dictionary = [{
    mercury: 'MERC'
  },
  {
    venus: 'VEN'
  },
  {
    earth: 'EART'
  },
  {
    mars: 'MAR'
  },
  {
    jupiter: 'JUP'
  },
  {
    saturn: 'SAT'
  },
  {
    uranus: 'ANUS'
  },
  {
    neptune: 'NEP'
  },
  {
    pluto: 'PLUT'
  },
];
var search = "m";
search = search.toLowerCase();
var output = Object.values(dictionary).map(s => Object.values(s)[0].toLowerCase()).filter(s => s.indexOf(search) == 0);
console.log( output );

Edit

To return in the original case, use toLowerCase before doing indexOf

var output = Object.values( dictionary ).map(
       s => Object.values( s )[0] ).filter(
         s => s.toLowerCase().indexOf( search ) == 0);

Upvotes: 3

Ele
Ele

Reputation: 33726

Using function filter to get your targets.

var dictionary = [
  { mercury: 'MERC' },
  { venus: 'VEN' },
  { earth: 'EART' },
  { mars: 'MAR' },
  { jupiter: 'JUP' },
  { saturn: 'SAT' },
  { uranus: 'ANUS' },
  { neptune: 'NEP' },
  { pluto: 'PLUT' }
];

let word = 'm';
let result = dictionary.filter(d => Object.values(d)[0].toLowerCase().indexOf(word) > -1)


 console.log(result)

If you want the values, use map function.

var dictionary = [
  { mercury: 'MERC' },
  { venus: 'VEN' },
  { earth: 'EART' },
  { mars: 'MAR' },
  { jupiter: 'JUP' },
  { saturn: 'SAT' },
  { uranus: 'ANUS' },
  { neptune: 'NEP' },
  { pluto: 'PLUT' }
];

let word = 'm';
let result = dictionary.filter(d => Object.values(d)[0].toLowerCase().indexOf(word) > -1)


 console.log(result.map((b) => Object.values(b)[0]))

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386654

You could filter the array and use just the values.

var dictionary = dictionary = [{ mercury: 'MERC' }, { venus: 'VEN' }, { earth: 'EART' }, { mars: 'MAR' }, { jupiter: 'JUP' }, { saturn: 'SAT' }, { uranus: 'ANUS' }, { neptune: 'NEP' }, { pluto: 'PLUT' }],
    word = 'm',
    result = dictionary
        .filter(d => Object.keys(d)[0].toLowerCase().startsWith(word))
        .map(o => Object.values(o)[0]);

console.log(result)

Upvotes: 4

Related Questions