#dictionary comprehension like in python

1 messages · Page 1 of 1 (latest)

quaint hollow
#

So im trying to create some sort of dict comprehension

i have an interface and list of objects following that interface and i want to create a dictionary where key is the object.Name and value is 0 for every element in list

#

in python it would look like this py {key.Name: 0 for key in mylist}

tough crow
#

"comprehension" exists in very few languages afaik

#

assuming you have an array, you could use Object.fromEntries(arr.map(...)) basically

#

and within map you'd have a callback turning each object into [name, 0]

#

you could also use arr.reduce to build that object gradually

#

or just use a normal loop to do the same as reduce

quaint hollow
#
multipliers: {[key: string]: number} = Object.fromEntries(listOfFood.map(food => [food.Name, 0]))
``` should work?
tough crow
#

sure

#

why is Name in PascalCase though

#

generally those should be in camelCase

quaint hollow
#

🫡

rain sundial
#

!resolved