'How to loop though Record<K, T>

I have Records type of Record:

export interface List {
  name: string;
  title: string;
}

export type RecordType
= 'recordOne'
| 'recordTwo'
| 'recordThree';

export const Records: Record<RecordType, List> = {
  recordOne: {
    name: 'Name1',
    title: 'Ausi bere ut erit adeo enim an suae'
  },
  recordTwo: {
    name: 'Name2',
    title: 'Petebat proprie suo methodo'
  },
  recordThree: {
    name: 'Name3',
    title: 'Petebat proprie suo methodo inscitiae'
  }
}

I want to search for record with specific text but in order to do that I need to loop through the Records so how would you do that? I mean how would you loop thought the Records ?

Basically that is what I want:

findMatchingTitle(myString) {
   let title = '';
   this.Records.foreach(x => {

     if(myString.includes(x.title)) {
       title = x.title;
     }
   });

  return title;
}

Any ideas?



Solution 1:[1]

Generally to loop through Objects you can do:

for(let prop in obj) console.log(obj[prop])

However typescript wont let you do that with no implicit any, which is why you'd have to type cast:

for (let prop in Records) console.log((Records as any)[prop]);

Another way to loop through an Object and compare properties is with Object.keys and Object.values as shown below

function findMatchingTitle(myString: string): string {
  for (let index in Object.keys(Records)) {
    let title: string = Object.values(Records)[index].name;
    if (title.includes(myString))
      return title;
  }
  return '';
}

Alternatively, for an exact search it would be:

function findMatchingTitle(myString: string): string {
  for (let index in Object.keys(Records)) {
    let title: string = Object.values(Records)[index].name;
    if (title === myString)
      return title;
  }
  return '';
}

Solution 2:[2]

Not really related to TS, in order to search for the title, I would first get the records values as array, and use the Array.find to look for the title.

if I found one, I will get its title

const records = {
  recordOne: {
    name: 'Name1',
    title: 'Ausi bere ut erit adeo enim an suae'
  },
  recordTwo: {
    name: 'Name2',
    title: 'Petebat proprie suo methodo'
  },
  recordThree: {
    name: 'Name3',
    title: 'Petebat proprie suo methodo inscitiae'
  }
}

const findMatchingTitle = str => Object.values(records).find(record => record.title.includes(str))?.title

console.log(findMatchingTitle("inscitiae"))

Solution 3:[3]

You can use Object.keys and Object.values to loop through the Record< RecordType, List> type.

Just use Object.values to return all the values on the Records and then filter by the title as follows:

function findMatchingTitle(title: string): List {
  return Object.values(Records).find((i: List) => i.title === title);
};

Would strongly recommend defining your argument and function return types in typescript as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mike S.
Solution 2 naortor
Solution 3 Santi Barbat