Junior — Senior
Create an object with subject statistics by regions
livecode
Task condition
The task is to implement a function that takes an array of objects, each containing properties name and region. The function should return an object where each key represents a region number, and the corresponding value is the count of subjects belonging to that region.
Additional variables and auxiliary arrays cannot be used. It is necessary to return the result directly from the getResult function.
interface ISubjectRegion {
name: string;
region: number;
}
interface IResult {
[key: ISubjectRegion['region']]: number;
}
const sourceData: ISubjectRegion[] = [
{ name: "Subject 1", region: 1 },
{ name: "Subject 2", region: 2 },
{ name: "Subject 3", region: 2 },
{ name: "Subject 4", region: 2 },
{ name: "Subject 5", region: 4 },
];
// expected result:
// {
// 1: 1, // 1 subject in region 1
// 2: 3, // 3 subjects in region 2
// }
function getResult(data: ISubjectRegion[]): IResult {
// implement here
}