RDW dataset - RowinRuizendaal/frontend-data GitHub Wiki

/**
 * Get data from a single column
 * @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 getColumn = (dataset, column) => {
  return dataset.map((result) => result[column]);
};

Function to get the value one certain column of a dataset, that returns the value of the dataset column.

/**
 * Get data from multiple columns
 * @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;
  });
};

Function to get the values of multiple columns of a dataset, that returns the specified values of the dataset columns.

/**
 * 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 [el.areaid, el.location.latitude, el.location.longitude];
  } );
};

Function to get the latitude and longitude from a specific dataset and it returns the areaid, latitude & longitude

/**
 * 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;
});

Function where we can combine datasets with on basis of a specific keyword (primairy key)

const pricePerHour = ((dataset) => {
  return dataset.map((el) => {
    return {
      areamanagerid: el.areamanagerid,
      pricePerHour: roundToTwoDecimals(el.amountfarepart / el.stepsizefarepart * 60),
    };
  } );
});

Calculate the PricePerHour based on the amountfarepart and the stepsizfarpart.

// https://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-only-if-necessary
const roundToTwoDecimals = (((number) => {
  return number.toFixed(2);
}));

Function to give back the number to fixed to 2 digits