'TypeScript: How to specify a reference column and copy its value to remaining columns?
I have a table that looks like this:
+-------+-------+-------+-------+-------+
| Week1 | Week2 | Week3 | Week4 | Week5 |
+-------+-------+-------+-------+-------+
| AAAAA | BBBBB | CCCCC | DDDDD | EEEEE |
+-------+-------+-------+-------+-------+
I would like to write a function that:
- Input: (1) reference column name (2) a list of impacted column name
- Output: impacted column values changed to the same as the reference column
For example: function(1,2) means: copy Week 1 value to Week 2
+-------+-------+-------+-------+-------+
| Week1 | Week2 | Week3 | Week4 | Week5 |
+-------+-------+-------+-------+-------+
| AAAAA | AAAAA | CCCCC | DDDDD | EEEEE |
+-------+-------+-------+-------+-------+
For another example:
function(1,[2,3]) means: copy Week 1 value to Week 2 and Week 3
+-------+-------+-------+-------+-------+
| Week1 | Week2 | Week3 | Week4 | Week5 |
+-------+-------+-------+-------+-------+
| AAAAA | AAAAA | AAAAA | DDDDD | EEEEE |
+-------+-------+-------+-------+-------+
For another example: function(4,[2,3,5]) means: copy Week 4 value to Week 2, Week 3, and Week 5
+-------+-------+-------+-------+-------+
| Week1 | Week2 | Week3 | Week4 | Week5 |
+-------+-------+-------+-------+-------+
| AAAAA | DDDDD | DDDDD | DDDDD | DDDDD |
+-------+-------+-------+-------+-------+
For another example: function(2,[1,3,5]) means: copy Week 2 value to Week 1, Week 3, and Week 5
+-------+-------+-------+-------+-------+
| Week1 | Week2 | Week3 | Week4 | Week5 |
+-------+-------+-------+-------+-------+
| BBBBB | BBBBB | BBBBB | DDDDD | BBBBB |
+-------+-------+-------+-------+-------+
There could be only 1 reference column, but there could be multiple impacted columns.
I am very new to TypeScript and can't wrap my head around on how to do this in TypeScript.
I am not sure if thinking this way makes sense:
- Concatenate all of 5 Weeks' of value into a long string (25 digits)
- Then I am able to have the index of each week (e.g. Week 1 value = str[0:4], Week 2 value = str[5:9] etc.)
- Then just take the reference column value and replace to the impacted columns
Much appreciation for any help!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
