-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ru Chern Chong
committed
Nov 1, 2023
1 parent
65d46cf
commit b92f3f6
Showing
4 changed files
with
47 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,29 @@ | ||
export const transformDataToDatasets = (inputData) => { | ||
const transformedData = inputData.map((item) => ({ | ||
label: item.make, | ||
data: [item.number], | ||
import type { Car, Dataset } from "@/types"; | ||
|
||
export const transformDataToDatasets = (inputData: Car[]): Dataset[] => { | ||
const transformedData: Dataset[] = inputData.map(({ make, number }: Car) => ({ | ||
label: make, | ||
data: [number], | ||
})); | ||
|
||
const datasets = transformedData.reduce((acc, item) => { | ||
const existingDataset = acc.find((dataset) => dataset.label === item.label); | ||
if (existingDataset) { | ||
existingDataset.data.push(item.data[0]); | ||
} else { | ||
acc.push(item); | ||
} | ||
return acc; | ||
}, []); | ||
const datasets: Dataset[] = transformedData.reduce( | ||
(acc: Dataset[], item: Dataset) => { | ||
const existingDataset = acc.find( | ||
(dataset) => dataset.label === item.label, | ||
); | ||
|
||
if (existingDataset) { | ||
existingDataset.data.push(item.data[0]); | ||
} else { | ||
acc.push(item); | ||
} | ||
|
||
return acc; | ||
}, | ||
[], | ||
); | ||
|
||
console.log({ datasets }); | ||
|
||
return datasets; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters