I am faced with the following code challenge:
runA() {
const request = [];
for (const item of this.items ) {
request.push(this.getRequestData(item.prop1)); // want to generalize that
}
return forkJoin(...request).pipe(x => x);
}
runB() {
const request = [];
for (const item of this.items ) {
request.push(this.getOtherData(item.prop2)); // want to generalize that
}
return forkJoin(...request).pipe(x => x);
}
I am looking to refactor this code in order to pass different arguments into request.push()
to avoid duplicating loops. The item
object contains various properties, so in some cases I require item.prop1
, while in others I need item.prop2
. Therefore, my goal is to have the final code structured as follows:
run(param) {
const request = [];
for (const item of this.items ) {
request.push(param); // need to find the correct way to handle this
}
return forkJoin(...request).pipe(x => x);
}
runA() {
run(this.getRequestData(item.prop1)) // need to get this right
}
runB() {
run(this.getOtherData(item.prop2)) // need to get this right
}
Is there a method to achieve this efficient refactoring in TypeScript?