Skip to content

Instantly share code, notes, and snippets.

@BrycePearce
Created August 8, 2016 23:53
Show Gist options
  • Save BrycePearce/d5238138ef9cde1cd1bbe0ea1702280c to your computer and use it in GitHub Desktop.
Save BrycePearce/d5238138ef9cde1cd1bbe0ea1702280c to your computer and use it in GitHub Desktop.
jsontobh with classes
'use strict';
//http://www.tutorialspoint.com/cplusplus/cpp_classes_objects.htm
//this script extracts source/destination values from .JSON (dataOut.json)
let obj = require('./dataOut.json');
var fs = require('fs');
const source = [];
const destination = [];
//block is each section of the array
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;
}
};
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];
}
}
});
});
//destination
obj.forEach(block => {
destination.push({
id: Values.dest_lat + Values.dest_lng,
"destination-lat": Values.dest_lat,
"destination-lng": Values.dest_lng,
"destination_address": Values.dest_address,
x: {
valueOf: function () {
var latlng = [
Values.dest_lat,
Values.dest_lng
];
var xy = map.FUNCTION_FOR_CONVERTING_LAT_LNG_TO_X_Y(latlng);
return xy[0];
}
},
y: {
valueOf: function () {
var latlng = [
dest_lat,
dest_lng
];
var xy = map.FUNCTION_FOR_CONVERTING_LAT_LNG_TO_X_Y(latlng);
return xy[1];
}
}
});
});
//write out and stringify
fs.writeFile('./parentSource.json', JSON.stringify(source, null, 2), 'utf-8');
fs.writeFile('./parentDestination.json', JSON.stringify(destination, null, 2), 'utf-8');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment