I am trying to create a table of all US states. Instead manually creating the table, I thought I would try using Javascript to create the table using data from a JSON file.
I have this jQuery function that is returning the JSON as an object. When I call console.log, I can see the JSON data like so:
0: {name: "Alabama", abbreviation: "AL"}
1: {name: "Alaska", abbreviation: "AK"}
2: {name: "American Samoa", abbreviation: "AS"}
3: {name: "Arizona", abbreviation: "AZ"}
4: {name: "Arkansas", abbreviation: "AR"}
5: {name: "California", abbreviation: "CA"}
6: {name: "Colorado", abbreviation: "CO"}
7: {name: "Connecticut", abbreviation: "CT"}
8: {name: "Delaware", abbreviation: "DE"}
9: {name: "District Of Columbia", abbreviation: "DC"}
10: {name: "Federated States Of Micronesia", abbreviation: "FM"}
11: {name: "Florida", abbreviation: "FL"}
12: {name: "Georgia", abbreviation: "GA"}
13: {name: "Guam", abbreviation: "GU"}
14: {name: "Hawaii", abbreviation: "HI"}
15: {name: "Idaho", abbreviation: "ID"}
16: {name: "Illinois", abbreviation: "IL"}
17: {name: "Indiana", abbreviation: "IN"}
18: {name: "Iowa", abbreviation: "IA"}
19: {name: "Kansas", abbreviation: "KS"}
I have tried storing that as a variable to then transfer to an array and then I loop through that array to create the table but it doesn't recognise the variable outside of that jQuery function.
$(document).ready(function () {
$.getJSON('resources/js/states.js ', function (data) {
console.log(data);
});
});
I am hoping someone can show me how to store the incoming JSON data as an array so that I can follow some other examples on creating a table by looping through that array.