javascript - Modifying code to use classes -


i created script here extracts data .json , creates object it. code works, need add source , destination class can export , use other files.

so rewrote code store data in class here. relevant portions show below:

const source = []; const destination = [];  class values {     constructor(source_address, source_lat, source_lng,         dest_address, dest_lat, dest_lng, scan_type) {         this.source_address = source_address;         this.source_lat = source_lat;         this.source_lng = source_lng;         this.dest_address = dest_address;         this.dest_lat = dest_lat;         this.dest_lng = dest_lng;         this.scan_type = scan_type;     } }; //block each section of array obj.foreach(block => {     source.push({         id: values.source_lat + values.source_lng,         "source-lat": values.source_lat,         "source-lng": values.source_lng,         "source_address": values.source_address,         x: {             valueof: function () {                 var latlng = [                     values.source_lat,                     values.source_lng                 ];                 var xy = map.function_for_converting_lat_lng_to_x_y(latlng);                 return xy[0];             }         },         y: {             valueof: function () {                 var latlng = [                     values.source_lat,                     values.source_lng                 ];                 var xy = map.function_for_converting_lat_lng_to_x_y(latlng);                 return xy[1];             }         }     }); }); 

but returns:

[   {     "id": null,     "x": {},     "y": {}   } ] 

why data values reading null? how should approach fixing problem?

you aren't initialising values? how expect return anything?

before loops need initialise data, so:

var valueclass = new value(--your constructor values here--); 

doing you'll able access class values, e.g valueclass.dest_address.

an example small snippet of code:

var valueclass = new value(--your constructor values here--);  //block each section of array obj.foreach(block => {     source.push({         id: valueclass.source_lat + valueclass.source_lng, 

Comments