I am tying to understand format of typescript/javascript and need help in understanding certain steps here. I am parsing a JSON file into an object and then trying to read values to update some values.
Thanks to Timmy chan I got some pointers from my previous post here, now building on that. Objects creation using interface for JSON types in typescript
Say I have this interface defined,
interface AllData {
[value:string]:Row
}
interface Row {
eDate: string;
mystate: string;
mystateCity: string;
numberofpeople: number;
}
let Data: AllData ;
I read the file like this and assign it to Data variable.
const response = await fetchFile(fileName);
Data= await response.json();
Now I want to create a new object which will only have rows
const CountData: Row[] = {} // this gives error Type '{}' is missing the following properties from type 'CountyGraphRow[]': length, pop, push,
if I change it to
const CountData: Row[] = [] // when to have {} or [].. What is the difference?
for (const rowData in Data["value"])
{
console.log(rowData); // this is coming out 0
CountData.push({ // error at TypeError: Cannot read property 'eDate' of undefined
eDate: Data[rowData].eDate, // Something is wrong in the way I am accessing them.
mystate: Data[rowData].mystate,
mystateCity: Data[rowData].mystateCity,
numberofpeople: Data[rowData].numberofpeople > 20? 20 : Data[rowData].numberofpeople < 5? 5: Data[rowData].numberofpeople,
})
}
Here is how the file looks like
{
"value": [
{
"eDate": "2020-03-01T00:00:00.000Z",
"mystate": "state1",
"mystateCity": "state1, ID",
"numberofpeople": 2.973529602
},
{
"eDate": "2020-03-02T00:00:00.000Z",
"mystate": "state1",
"mystateCity": "state1, ID",
"numberofpeople": 2.973529602
},
{
"eDate": "2020-03-03T00:00:00.000Z",
"mystate": "state1",
"mystateCity": "state1, ID",
"numberofpeople": 2.973529602
}
]}
--------------Update 2-------------
interface AllData {
[value: string]: Row
}
interface Row {
eDate: string;
mystate: string;
mystateCity: string;
numberofpeople: number;
}
let Data: AllData;
Data = JSON.parse(`{
"value": [
{
"eDate": "2020-03-01T00:00:00.000Z",
"mystate": "state1",
"mystateCity": "state1, ID",
"numberofpeople": 2.973529602
},
{
"eDate": "2020-03-02T00:00:00.000Z",
"mystate": "state1",
"mystateCity": "state1, ID",
"numberofpeople": 2.973529602
},
{
"eDate": "2020-03-03T00:00:00.000Z",
"mystate": "state1",
"mystateCity": "state1, ID",
"numberofpeople": 2.973529602
}
]}`);
const CountData: Row[] = [];
for (const rowData in Data["value"]) { // Data["value"] I am assuming this //will pick all objects "value": [
// {
// "eDate": "2020-03-01T00:00:00.000Z",
// please correct me if I am wrong
console.log(rowData); // this is coming out 0
CountData.push({ // error at TypeError: Cannot read property 'eDate' of undefined
eDate: Data[rowData].eDate, // Something is wrong in the way I am accessing them.
mystate: Data[rowData].mystate,
mystateCity: Data[rowData].mystateCity,
numberofpeople: Data[rowData].numberofpeople > 20 ? 20 : Data[rowData].numberofpeople < 5 ? 5 : Data[rowData].numberofpeople,
})
}
CountGraph? What iscountData(lowercasecmeans it is notCountData)? Why are you getting the property ofDatawhose key is"value"? Do you even know if there is a"value"property in there? (Oh, I guess you do know that, butDatais not properly typed.) Please consider getting providing a minimal reproducible example version of the code that demonstrates your issue when dropped into a standalone IDE like The TypeScript Playground.{}is an empty object, whereas[]is an empty list. By default, JavaScript lists havelength,push, andpopproperties, so using[]in this case matches up with the specified type (Row[], a list ofRows).{}has no properties, so you're getting an error saying that variable should have the required properties to line up with theRow[]interface. As for the rest, it is a bit hard to tell what is going on. You should edit the question and provide a bit more information, as jcalz suggested :)Datais mistyped), ask it. Good luck!