Welcome to the blog of Mr.G
<script>
/* 2. Grouping Orders */
/* Orders Array */
const orders = [
{ countryCode: 'US', name: 'John', product: 'stickers' },
{ countryCode: 'CA', name: 'Sarah', product: 'shirt' },
{ countryCode: 'MX', name: 'Carlos', product: 'skateboard' },
{ countryCode: 'FR', name: 'Marie', product: 'hoodie' },
{ countryCode: 'JP', name: 'Yuki', product: 'basketball' },
{ countryCode: 'US', name: 'Emily', product: 'stickers' },
{ countryCode: 'US', name: 'Scott', product: 'shirt' },
{ countryCode: 'CA', name: 'David', product: 'shirt' },
{ countryCode: 'MX', name: 'Juan', product: 'skateboard' }
];
/* Using reduce to Group Orders */
const ordersByCountryR = orders.reduce((acc, order) => {
const { countryCode } = order;
if (!acc[countryCode]) {
acc[countryCode] = [];
}
acc[countryCode].push(order);
return acc;
}, {});
console.log(ordersByCountryR);
/* Group Orders by Product */
const { shirt, stickers } = Object.groupBy(orders, (order) => order.product);
console.log("shirt\n", shirt);
/* Ouput */
// [
// {
// "countryCode": "CA",
// "name": "Sarah",
// "product": "shirt"
// },
// {
// "countryCode": "US",
// "name": "Scott",
// "product": "shirt"
// },
// {
// "countryCode": "CA",
// "name": "David",
// "product": "shirt"
// }
// ]
console.log("stickers\n", stickers);
/* Group Orders by Country Code */
const ordersByCountry = Object.groupBy(orders, (order) => order.countryCode);
console.log("ordersByCountry\n", ordersByCountry);
/* Grouped Orders by Country Code: */
/* {
"US": [
{ countryCode: "US", name: "John", product: "stickers" },
{ countryCode: "US", name: "Emily", product: "stickers" },
{ countryCode: "US", name: "Scott", product: "shirt" }
],
"CA": [
{ countryCode: "CA", name: "Sarah", product: "shirt" },
{ countryCode: "CA", name: "David", product: "shirt" }
],
"MX": [
{ countryCode: "MX", name: "Carlos", product: "skateboard" },
{ countryCode: "MX", name: "Juan", product: "skateboard" }
],
"FR": [{ countryCode: "FR", name: "Marie", product: "hoodie" }],
"JP": [{ countryCode: "JP", name: "Yuki", product: "basketball" }]
}
*/
/* 1. Grouping Topics and Utterances
Topics Array */
const topics = [
{ topic: 'Topic 1', start: 0 },
{ topic: 'Topic 2', start: 12 },
{ topic: 'Topic 3', start: 24 },
];
/* Utterances Array */
const utterances = [
{ text: 'Hello Welcome to the show', start: 0, end: 3 },
{ text: 'JavaScript popular for web dev', start: 4, end: 7 },
{ text: 'Variables declared with var, let', start: 8, end: 11 },
{ text: 'Prototype inheritance in JavaScript', start: 12, end: 15 },
{ text: 'DOM API manipulates HTML', start: 16, end: 19 },
{ text: 'Callbacks used for async code', start: 20, end: 23 },
{ text: 'Runs client-side and server-side', start: 24, end: 27 },
{ text: 'React and Vue popular frameworks', start: 28, end: 31 },
{ text: 'Node.js runs JavaScript outside browser', start: 32, end: 35 }
];
/*
Group Utterances into Topics */
const groupedTopics = Map.groupBy(utterances, (utterance) =>
topics.find((topic, i) => {
const { start } = topic;
const end = topics.at(i + 1)?.start ?? Infinity;
return utterance.start >= start && utterance.end <= end;
})
);
console.log(groupedTopics);
/* Output */
/* Map(3) {
"Topic 1" => [
{ text: "Hello Welcome to the show", start: 0, end: 3 },
{ text: "JavaScript popular for web dev", start: 4, end: 7 },
{ text: "Variables declared with var, let", start: 8, end: 11 }
],
"Topic 2" => [
{ text: "Prototype inheritance in JavaScript", start: 12, end: 15 },
{ text: "DOM API manipulates HTML", start: 16, end: 19 },
{ text: "Callbacks used for async code", start: 20, end: 23 }
],
"Topic 3" => [
{ text: "Runs client-side and server-side", start: 24, end: 27 },
{ text: "React and Vue popular frameworks", start: 28, end: 31 },
{ text: "Node.js runs JavaScript outside browser", start: 32, end: 35 }
]
} */
</script>
Lem - Warren Buffett walked into a University of Florida classroom in 1998 ... [2098423133938356224] (1)
Sam Stoffel - Jack Dorsey's simple rule for success in business [2089319019065057280]
Peter-Steinberger-What-Happens-When-4.7-_1786407922546
Monitor_server_action____
Disk_and_RAID_testing_commandsWebDev New York London AI NORWAY DIY Ideas Life Family News Clothing Invest Sports Health