transform data - RowinRuizendaal/frontend-data GitHub Wiki
Data transformations
Getting multiple columns
/**
* Get multiple columns out of a dataset
* @param {array} dataset - Array containing dataset
* @param {array} columns - Array containing the names of the columns you want
* @return {array} array containing values from the selected column
*/
const getColumns = (dataset, columns) => {
return dataset.map((value) => {
const data = {};
for (let i = 0; i < columns.length; i++) {
data[columns[i]] = value[columns[i]];
}
return data;
});
};
Select a array dataset and pick the columns you want out of that dataset
Linking datasets
/**
* Combine 2 datasets based on a primary key value
* @param {array} dataset1 - Array containing dataset
* @param {array} dataset2 - Array containing dataset
* @param {string} keyword - key you want to combine the data with
* @return {array} A new object with the two combined datasets
*/
// worked together with Sam to refactor the code:
// https://github.com/Vuurvos1/functional-programming/blob/main/modules/dataHelpers.js#L114-L140
const combineDataset = ((dataset1, dataset2, keyword) => {
const values = [];
for (const i of dataset1) {
let output = {};
// Find match between the datasets and keyword
const findMatch = dataset2.find((element) => {
return i[keyword] == element[keyword];
});
// if match return true
if (findMatch) {
// store value in output
output = findMatch;
// Loop over the other items are that stored in other sets
for (const [keyword, value] of Object.entries(i)) {
// If it doesnt exist store it
if (!output[keyword]) {
output[keyword] = value;
}
}
values.push(output);
} else {
// Nothing was found to match with
}
}
return values;
});
Combine 2 datasets bad on a primary key, the function returns a new array with the values that are link with each other
const pricePerHour = ((dataset) => {
return dataset.map((el) => {
return {
areamanagerid: el.areamanagerid,
pricePerHour: roundToTwoDecimals(el.amountfarepart / el.stepsizefarepart * 60),
};
} );
});
// https://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-only-if-necessary
const roundToTwoDecimals = (((number) => {
return number.toFixed(2);
}));
Calculates the price per hour, and return it into a object
/**
* Get data from multiple columns
* @param {array} dataset - Array containing dataset
* @return {array} array containing values from the location column
*/
// location -> latitude & longitude
const getCoords = (dataset) => {
return dataset.map((el) => {
return {
areaid: el.areaid,
latitude: el.location.latitude,
longitude: el.location.longitude,
};
} );
};
Returns areaid, latitude, longitude from a specific dataset some values are being skipped
check if values are unique
const duplicate = ((dataset) => {
return [...new Set(dataset)];
});
This returns a new array set with unique values
End result of my json structure
[{
"areamanagerid": "988",
"areaid": "988_STAT",
"paymentmethod": "pin",
"pricePerHour": "0.00",
"areadesc": "Parkeergarage Stationsplein (Weert)",
"location": {
"longitude": "5.705462804",
"latitude": "51.249263663"
}
}, {
"areamanagerid": "268",
"areaid": "268_PRNOO",
"paymentmethod": "Maestro",
"pricePerHour": "1.05",
"areadesc": "Garage Keizer Karel (Nijmegen)",
"location": {
"longitude": "5.857931044",
"latitude": "51.842372259"
}
}