I need to programmatically create dicts with 'name' and 'path' key values and store them in the items list
const state = {
items: [
{
name: user.name,
path: user.path,
},
{
name: user.name,
path: user.path,
},
]
}
The example above is all I'm trying to do here, its just that I need to be able to programmatically create the dicts inside the list.
To break this down into steps, I believe what I need to do is:
- loop through each element in a list
- create a dict from the name/path of that element
- append the dict to a list
I am coming from a Python background so not completely sure how to do this in .js and these steps may not be correct.
The most important part, is that I need the structure above to remain as this is being called/ingested by another js module
Assuming we have a list that looks something like this:
[ user1: {name: 'ben'}, user2: {path: 'none'}]
What would be the cleanest way to loop through this and create the main example above ?
Also side question for bonus points, can I just create a list named somelistname and append the dictionaries to the list and then do
const state = {
items: somelistname
}
would that be the same thing as my example?
[ user1: {...}, ...]one), so is that a typo?. And for your side question, yes you can absolutely do that.[ user1: {name: 'ben'}, user2: {path: 'none'}]is not a valid array.