Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaScript: Array, Loop, Data File

Similar presentations


Presentation on theme: "JavaScript: Array, Loop, Data File"— Presentation transcript:

1 JavaScript: Array, Loop, Data File

2

3 <!DOCTYPE html> <html>   <head>     <style>        #map {         height: 400px;         width: 100%;        }     </style>   </head>   <body>     <h3>My Google Maps Demo</h3>     <div id="map"></div>     <script>       function initMap() {         var uluru = {lat: , lng: };         var map = new google.maps.Map(document.getElementById('map'), {           zoom: 4,           center: uluru         });         var marker = new google.maps.Marker({           position: uluru,           map: map         });       }     </script>     <script async defer     src="     </script>   </body> </html>

4 function initMap() { var uluru = {lat: -25. 363, lng: 131
function initMap() { var uluru = {lat: , lng: }; var map = new google.maps.Map(document.getElementById('map'), { zoom: 4, center: uluru }); var marker = new google.maps.Marker({ position: point1, map: map }); var marker = new google.maps.Marker({ position: point2, map: map }); . . }

5 A Better Idea: Array + Loop

6 Array loop from the 1st item to the last item { create a marker; }
A data structure to hold a set of data with the same attributes Benefit of Using Array Repetition becomes easier. loop from the 1st item to the last item { create a marker; }

7 Array in JavaScript Simple array: elements are a simple data type.
var aListofNumber = [1,2,3,4]; var aListofString = ["a", "b", "c","d"];

8 Array Reference Use an indexing number ArrayName[indexNumber]
var aListofNumber = [1,2,3,4]; var aListofString = ["a", "b", "c","d"]; aListofNumber[0] aListofString[3]

9 Array in JavaScript Object array: elements are objects, which have multiple data attributes (same or different types). var aListofPeople = [{name: "John", age: 28}, {name: "Jane", age: 24}, {name: "Joe", age: 20}]; Objects are grouped by {}. aListofPeople[1].name aListofPeople[2].age

10 In the Case of Your Assignment
crimes = [ {date: "9/1/2016", lat: 40.81, lng:-77.86, location: "Wagner Bld"}, {date: "9/1/2016",lat: 40.79, lng: , location: "Reber Bld“}, . ];

11 Array Properties Most useful one: length
arrayName.length var aListofNumber = [1,2,3,4]; aListofNumber.length = 4;

12 Array Methods Some important methods push(): adding a new element
pop(): removing the last element indexOf(): finding the position of a given element forEach(): calling a function for each element

13 Processing Data in An Array
Loop The For loop for (statement 1; statement 2; statement 3) {     code block to be executed } for (var i=0; i<crimes.length; i++) { var marker = new google.maps.Marker({           position: ???,           map: map         }); }

14 Peer Help Assisting peers and learning from peers are very helpful.
If you finish your exercise, feel free to help others. Put your name on the Peer-Helper sheet after the class. Used to boot your class participation grade.

15 Exercise 1 Convert the entries in the text file into an array of objects for all crimes records. Each record should have at least the following attributes Date, Offenses, Location, lat, lng Hint: Adding necessary attribute names and characters in Excel. Copying the data into JavaScript. To check your data in JavaScript for (var i=0; i<crimes.length; i++) { console.log(crimes[i]); });

16 Exercise 2 Based on the codes for Exercise 1, display all markers.

17 Exercise 3 (Optional) Use the forEach() method to process the marker data An example can be found here: Hint crimes.forEach(function(crime) { … })

18 Data File Converting the raw data into array is cumbersome.
Can we read the file directly? Yes! There are many ways to do that. D3.js offers a simpler approach. To read a csv file or a JSON file d3.csv ("filename", function(data) {}); CSV: all data is string d3.json ("filename", function(data) {}); JSON: smarter, different data types. E.g., latitude and longitude data The data file should be on a server (could be the same server with the js file).

19 d3. csv("crimes. txt", function(data) { console
d3.csv("crimes.txt", function(data) { console.log(data); }); For JSON file, you can convert the csv file to JSON with some online tools, such as d3.json("crimes.json", function(data) { Pay attention to the difference in latitude and longitude data. Need this line before calling d3.csv or d3.json : <script src="//d3js.org/d3.v3.min.js"> </script>

20 Exercise 4 (Optional) Read the data file and do the marker presentation.

21 Bonus Points Use an infowindow for each click on a marker.
Listen to the click event on a marker Call a function to show an inforwindow with appropriate information.

22 Update of Grading Rubric
Comments for JavaScript codes 1 point var uluru = {lat: ,lng: }; var map = new google.maps.Map(document.getElementById('map'), { zoom: 12, center: uluru }); //the center of the map //a new map with a given center and zooming level


Download ppt "JavaScript: Array, Loop, Data File"

Similar presentations


Ads by Google