I am trying to end up with a dictionary of list of dictionaries and I am having difficulty adding elements to it. What I have is a simple enough CSV, that looks like so in excel.
The above is parsed using papa.parse to be an array of dictionaries with the headers the keys of the dictionaries, but I need to format it in a specific way.
What I need to end up with is a dictionary whose first entry:
finalDict = {
state: "CA",
governor: {
first_name: "Gavin",
last_name: "Newsom"
},
cities: [{city1: "LA", population: "8M", mayor: "ABC"},
{city2: "SF", population: "1M", mayor: "DEF"}]
}
It seemed like it should be simple enough and I wrote a function that takes a file and iterates through but am having difficulty with the "cities" key above:
function parseFile(csv) {
let stateGovs = {
state: "",
governor: {
first_name:"",
last_name:""
},
cities:[]
}
// Note I can write more detail in my for loop upon request, but this is where the issue is
csv.forEach((entry) => {
// insert some more parsing logic, but I end up with a temp variable that looks like
temp = {city1: "LA", population: "8M", mayor: "ABC"}
// and I say
stateGovs.cities.push(temp)
}
}
The way I have defined cities in my dict, intellisense tells me it is of type never[] and when I try to push anything to it (namely my temp dictionary), I get the warning:
"argument of type any[]/{} is not assignable to parameter of type 'never'"
and that is where I am stuck.
I have tried making my temp variable a list a dict, explicitly defining a type outside my function, but the crux of the issue is this and nothing seems to work as I do not know (enough) of what I am doing wrong here. Any help would be appreciated.
