Created
May 19, 2022 12:57
-
-
Save avrcoelho/210deeb4dad4bd2ba070bdbe5e3e2f7e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| const transactions = [ | |
| { | |
| id: 3, | |
| sourceAccount: "A", | |
| targetAccount: "B", | |
| amount: 100, | |
| category: "eating_out", | |
| time: "2018-03-02T10:34:30.000Z", | |
| }, | |
| { | |
| id: 1, | |
| sourceAccount: "A", | |
| targetAccount: "B", | |
| amount: 100, | |
| category: "eating_out", | |
| time: "2018-03-02T10:33:00.000Z", | |
| }, | |
| { | |
| id: 6, | |
| sourceAccount: "A", | |
| targetAccount: "C", | |
| amount: 250, | |
| category: "other", | |
| time: "2018-03-02T10:33:05.000Z", | |
| }, | |
| { | |
| id: 4, | |
| sourceAccount: "A", | |
| targetAccount: "B", | |
| amount: 100, | |
| category: "eating_out", | |
| time: "2018-03-02T10:36:00.000Z", | |
| }, | |
| { | |
| id: 2, | |
| sourceAccount: "A", | |
| targetAccount: "B", | |
| amount: 100, | |
| category: "eating_out", | |
| time: "2018-03-02T10:33:50.000Z", | |
| }, | |
| { | |
| id: 5, | |
| sourceAccount: "A", | |
| targetAccount: "C", | |
| amount: 250, | |
| category: "other", | |
| time: "2018-03-02T10:33:00.000Z", | |
| }, | |
| ]; | |
| type Transaction = (typeof transactions)[0]; | |
| const transactionsGrouped = (tscts: Transaction[]) => { | |
| const transactionArray: Transaction[][] = []; | |
| tscts.forEach((tsct) => { | |
| const existsId = transactionArray.some((ts) => | |
| ts.some((t) => t.id === tsct.id) | |
| ); | |
| if (!existsId) { | |
| const transactionFinded = tscts.filter( | |
| (ts) => | |
| ts.amount === tsct.amount && | |
| ts.sourceAccount === tsct.sourceAccount && | |
| ts.category === tsct.category && | |
| ts.targetAccount === tsct.targetAccount | |
| ); | |
| transactionArray.push(transactionFinded); | |
| } | |
| }); | |
| return transactionArray; | |
| }; | |
| console.log(transactionsGrouped(transactions)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment