dataDesk BLOG

Welcome to the blog of Mr.G

Nature

/Dev

(Last modified: December 17 2024 02:16:56)

reduce

Here is the extracted code from all the images:


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);

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' }
];

Group Orders by Product

const { shirt, stickers } = Object.groupBy(orders, (order) => order.product);

console.log(shirt);

Group Orders by Country Code

const ordersByCountry = Object.groupBy(orders, (order) => order.countryCode);

console.log(ordersByCountry);

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);

Output Examples

  1. Grouped Topics Example:

    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 }
    ]
    }
  2. 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" }]
    }

Notes:

  • The Object.groupBy method is a modern way to group objects by a key. It works efficiently and cleanly.
  • The reduce method can achieve the same functionality but requires more manual setup.

Who I am

"Just me, myself, and I, exploring the universe of unknownment, embracing the mystery of what lies beyond, one star at a time."

Share this page on Facebook | X.com | Reddit

Latest Posts

  • Image Lem - Warren Buffett walked into a University of Florida classroom in 1998 ... [2098423133938356224] (1)
    Published: September 12 2026
    Read
  • Image Sam Stoffel - Jack Dorsey's simple rule for success in business [2089319019065057280]
    Published: August 19 2026
    Read
  • Image Peter-Steinberger-What-Happens-When-4.7-_1786407922546
    Published: August 16 2026
    Read
  • Image Monitor_server_action____
    Published: August 16 2026
    Read
  • Image Disk_and_RAID_testing_commands
    Published: July 31 2026
    Read

Tags

WebDev New York London AI NORWAY DIY Ideas Life Family News Clothing Invest Sports Health