0

I'm trying to convert an array of object to a map, indexed by an attribue value of the Object in typescript 4.1.5

Furthermore I want only the attributes of a certain type (here string)

A very similar question have been asked in javascript here : Convert object array to hash map, indexed by an attribute value of the Object

type Foo = {
        a : string
        b : number
}

const foos: Foo[] = []
// should only propose to index by 'a'
const foos_indexed_by_a = indexArrayByKey(foos, 'a')

function indexArrayByKey<T> (array: T[], key: Extract<keyof T, string>): Map<string, T> {
        return array.reduce((map, obj) => map.set(obj[key], obj), new Map<string, T>())
}

Playground Link

For now I can't access property key of obj (compilation failure)

Thanks for your help :)

Nainpo
  • 147
  • 1
  • 7

1 Answers1

2

To filter the keys based on which have a string value you can't use Extarct that filters the keys which are strings (not number or symbol). You can use KeyOfType described here

type KeyOfType<T, V> = keyof {
    [P in keyof T as T[P] extends V? P: never]: any
}
function indexArrayByKey<T, K extends KeyOfType<T, string>> (array: T[], key: K): Map<T[K], T> {
        return array.reduce((map, obj) => map.set(obj[key], obj), new Map<T[K], T>())
}

Playground Link

We use T[K] instead of string because T[K] could also be a subtype of string so TS would complain at string, but for cases this will work well.

Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357
  • I think you were right my answer was missing the point. The question clearly says *"indexed by an attribue value of the Object"*. *sigh* :-) – T.J. Crowder Jan 20 '22 at 09:20
  • Thanks your answer works really well in typescript v4.5.4 but not in the one I'm using v4.1.5. I should have specified it sorry. Do you have a solution for it ? – Nainpo Jan 20 '22 at 11:25
  • 1
    @Nainpo you can just swap out the definition of `KeyOfType` with the older one here https://stackoverflow.com/questions/51419176/how-to-get-a-subset-of-keyof-t-whose-value-tk-are-callable-functions-in-typ The rest should work. – Titian Cernicova-Dragomir Jan 20 '22 at 11:34
  • Playground link for older compatible version: https://www.typescriptlang.org/play?ts=4.1.5#code/C4TwDgpgBAYg9nKBeKBvAUFL2sEMoBcUAzsAE4CWAdgOaY7YBGhUVArgLaMRnoC+6dAGM4VUlABmCYkXhwA2gF1kUJegD06kgAs4bADYATKKP0goYMnDBxi0YImqGIADyiNzAclyfho8VK2APpOrhCGQR5B+CihLgCCZGS4IABCIADSECAAFIHEADRQ3p4AlOigkFBZIADyEgAq4BAAPA1FAKoAfCqo8gAKUNRQANbZcBJQDYpEDQPKrsAQVIbEUB1QAPxQ-URUEABuPHzyYyATU4roEmxUQsAUokMrronJaZnZbUUZUIvLq2q2XqTUg3xI5GoNC6PRyuCSKVmSiKZyIGVKRAAsrgwG15BlFEUGj0MAxsGQIMA2GQqFB4e8AHQUwxsIQQHI5Dg4opwRgAK1KyB6XLADLswByvL5p2yhJM-NKRX2AHcoNjcXMCUSujlSuUBEA – Titian Cernicova-Dragomir Jan 20 '22 at 11:35
  • Thanks for the new definition it's ok ! :) – Nainpo Jan 20 '22 at 13:21