To start I am new to AngularJS with some familiarity with Node/Javascript but have 25+ years programming. I am now supporting an AngularJS SPA and learning a lot, however there are some things I am not quite understanding, and don't know if this is an AngularJS thing, JSON thing or JavaScript thing.
Currently the SPA has it's data is hardcoded in a JSON file which is loaded with a $http.get
to the file. I now need to create the real API that the SPA will be calling for this data.
What perplexes me is the structure of the hardcoded data, here is a sample
{
"userId": "string",
"firstName": "string",
"lastName": "string",
"phoneNumber": 1234567890,
"phoneExtention": 123,
"faxNumber": 1234567890,
"email": "[email protected]",
"providers": {
"1386664670": {
"firstName": "string",
"middleInitial": "c",
"lastName": "string",
"description": "string",
"location": [{
"locationId": 123456789,
"name": "string",
"address": "string",
"city": "string",
"state": "string",
"postalCode": "string",
"phone": 1234567890
}]
},
"1548223027": {
"firstName": "string",
"middleInitial": "c",
"lastName": "string",
"description": "string",
"location": [{
"locationId": 123456789,
"name": "string",
"address": "string",
"city": "string",
"state": "string",
"postalCode": "string",
"phone": 1234567890
}, {
"locationId": 123456789,
"name": "string",
"address": "string",
"city": "string",
"state": "string",
"postalCode": "string",
"phone": 1234567890
}]
},
"1336340579": {
"firstName": "string",
"middleInitial": "c",
"lastName": "string",
"description": "string",
"location": [{
"locationId": 123456789,
"name": "string",
"address": "string",
"city": "string",
"state": "string",
"postalCode": "string",
"phone": 1234567890
}, {
"locationId": 123456789,
"name": "string",
"address": "string",
"city": "string",
"state": "string",
"postalCode": "string",
"phone": 1234567890
}]
}
}
}
what is odd to me is the fact that providers
is a collection of objects where the ID is the name of the object. I would have thought you would want the providers
to be an array of, well, providers like this:
{
"userId": "string",
"firstName": "string",
"lastName": "string",
"phoneNumber": 1234567890,
"phoneExtention": 123,
"faxNumber": 1234567890,
"email": "[email protected]",
"providers": [{
"npi": "1386664670",
"firstName": "string",
"middleInitial": "c",
"lastName": "string",
"description": "string",
"location": [{
"locationId": 123456789,
"name": "string",
"address": "string",
"city": "string",
"state": "string",
"postalCode": "string",
"phone": 1234567890
}]
}, {
"npi": "1548223027",
"firstName": "string",
"middleInitial": "c",
"lastName": "string",
"description": "string",
"location": [{
"locationId": 123456789,
"name": "string",
"address": "string",
"city": "string",
"state": "string",
"postalCode": "string",
"phone": 1234567890
}, {
"locationId": 123456789,
"name": "string",
"address": "string",
"city": "string",
"state": "string",
"postalCode": "string",
"phone": 1234567890
}]
}, {
"npi": "1336340579",
"firstName": "string",
"middleInitial": "c",
"lastName": "string",
"description": "string",
"location": [{
"locationId": 123456789,
"name": "string",
"address": "string",
"city": "string",
"state": "string",
"postalCode": "string",
"phone": 1234567890
}, {
"locationId": 123456789,
"name": "string",
"address": "string",
"city": "string",
"state": "string",
"postalCode": "string",
"phone": 1234567890
}]
}]
}
Both contain the same data it is just accessed differently. I was just going to redefine the JSON but I figured I better understand why it was done the way it was first. In the HTML/AngularJS the data is being iterated over via:
<md-tab ng-repeat="(key, value) in vm.providerInfo">
<md-tab-label>
{{vm.providerInfo[key].firstName}} {{vm.providerInfo[key].lastName}}
</md-tab-label>
But I think I could just as easily use this code:
<md-tab ng-repeat="provider in vm.providerInfo">
<md-tab-label>
{{provider.firstName}} {{provider.lastName}}
</md-tab-label>
if I restructured the data as described above.
Can anyone point out why I would not want to change the JSON structure to an array of providers?