//data
"locations": [
{
"name": "Business Academy Australia",
"lat": -33.8688,
"lng": 151.2093,
"website": "https://www.businessacademyaustralia.com.au",
"tel": "0290198888",
"email": "info@businessacademyaustralia.com.au",
"category": "Campus",
"governmentService": true,
"formattedLocality": "Sydney, NSW 2000",
"providerId": "13579",
"locationId": "4001",
"registeredTrainerId": "24680",
"isVetFeeProvider": false,
"locationTypeId": null,
"facilities": null,
"service": null,
"apprenticeshipTraineeship": true,
"minimumFee": 1400,
"maximumFee": 2800,
"isVetFeeCourse": false,
"isAvailableOnline": true,
"isAvailablePartTime": true,
"deliveryModes": ["In-person"]
},
{
"name": "Entrepreneur Education",
"lat": -27.4698,
"lng": 153.0251,
"website": "https://www.entrepreneureducation.com.au",
"tel": "0733332222",
"email": "info@entrepreneureducation.com.au",
"category": "Campus",
"governmentService": true,
"formattedLocality": "Brisbane, QLD 4000",
"providerId": "98765",
"locationId": "4002",
"registeredTrainerId": "13579",
"isVetFeeProvider": false,
"locationTypeId": null,
"facilities": null,
"service": null,
"apprenticeshipTraineeship": true,
"minimumFee": 3800,
"maximumFee": 4700,
"isVetFeeCourse": true,
"isAvailableOnline": false,
"isAvailablePartTime": false,
"deliveryModes": ["Online","Hybrid"]
},
{
"name": "Small Business Training Institute",
"lat": -27.9687807,
"lng": 153.4066696,
"website": "https://www.sbtinstitute.com.au",
"tel": "0388885555",
"email": "info@sbtinstitute.com.au",
"category": "Campus",
"governmentService": false,
"formattedLocality": "Melbourne, VIC 3000",
"providerId": "54321",
"locationId": "4003",
"registeredTrainerId": "67890",
"isVetFeeProvider": false,
"locationTypeId": null,
"facilities": null,
"service": null,
"apprenticeshipTraineeship": false,
"minimumFee": 2200,
"maximumFee": 4100,
"isVetFeeCourse": true,
"isAvailableOnline": true,
"isAvailablePartTime": true,
"deliveryModes": ["In-person"]
},
// using groupby to group locations based on the items in the delivery modes values
// shows array of arrays
const getDeliveryItems = items.map((item) => item.deliveryModes);
console.log({ getDeliveryItems });
//flatten all the arrays combined into one array and sort each one by their name key
const flattened = items.flatMap((item) => item.deliveryModes.map((mode) => ({ ...item, mode }))).sort((a, b) => a.name.localeCompare(b.name));
console.log({ flattened });
// use grouping to separate out by a new mode key
let modesGrouped = Object.groupBy(flattened, ({ mode }) => mode);
console.log({ modesGrouped });
// In-person: Array(5), Online: Array(4), Hybrid: Array(1)}
Object.keys(modesGrouped).forEach((key) => {
// remove the mode key by returing ...rest
modesGrouped[key] = modesGrouped[key].map(({ mode, ...rest }) => rest);
});
console.log({ modesGrouped }); //
// destructuring
const { Hybrid: hybrid, ["In-person"]: inPerson, Online: online } = modesGrouped;
console.log({ hybrid, inPerson, online });
// another example
const apprenticeshipValues = Object.groupBy(items, ({ apprenticeshipTraineeship }) => apprenticeshipTraineeship);
console.log(apprenticeshipValues[true]);
console.log(apprenticeshipValues[false]);
// // desctructuring the objects into an array
const { true: apprenticeshipTraineeship = [],
false: nonapprenticeshipTraineeship = [] } = apprenticeshipValues;
console.log({ apprenticeshipTraineeship });
console.log({ nonapprenticeshipTraineeship });
Comments