I have the following rxjs observable:
export interface SubExperimentData {
experimentId: number;
name: string;
subexperiment: {
name: String
avg: number;
max: number;
}[];
}
What i wanna do now is to get all subexperiment of a specific experiment. I use the following code to get all subexperimentdata from one experiment.
choosedExperiment is containing the selected experiment which subexperiment i wanna get.
this.subexperimentData.pipe(map((extractedSResult) => {
extractedSResult.reduce((extractedSResult) => { //Error
if(extractedSResult.name == this.choosedExperiment) {
extractedSResult.subexperiment.forEach((t) => {
this.SubTaskAvg.push(t.avg)
this.SubTaskName.push(t.name)
}
)
}
}
)
}
)).subscribe();
});
Error: TS2345: Argument of type '(extractedSResult: SubExperimentData) => void' is not assignable to parameter of type '(previousValue: SubExperimentData, currentValue: SubExperimentData, currentIndex: number, array: SubExperimentData[]) => SubExperimentData'. Type 'void' is not assignable to type 'SubExperimentData'.
Im not really sure why this errors happen. Unfortunately, I do not know where the mistakes come from. For a professional it may be clear but as a newcomer I do not understand it. The basis of the code comes from another problem which I have already solved. Unfortunately, I understand the code only 90% Why I do not know the solution to the problem. Thank you!
CodePudding user response:
It's not clear what you're trying to accomplish
What is the type of extractedSResult
? The fact that you have extractedSResult.reduce
makes me believe it's an iterable (Maybe an array of some kind?) If so, maybe pluralize the variable so that it's extractedSResults
. You shadow the same variable name in the lambda you pass to reduce. What's the type of extractedSResult
there? Looks like maybe SubExperimentData
is the type.
Inside reduce you're doing something to some global variables. Looks like you're accumulating values in two data structures of some sort (A stack or an array etc). But you're not returning anything. That means this function's return type is void
.
What your error means
The error is telling you that the lambda that you're passing to reduce is bad. Look up how reduce works, it needs a lamda where the first value is an accumulated value and the second value is the value of the current iteration (often you'll need a seed value for the accumulated value too).
Finally, reduce also expects to have a return type. Specially, it needs to return the new accumulated data.
Your lambda isn't meeting any of these conditions.
Toward a solution
It's hard to determine what you're attempting to do here. If all you're after are the side effects, you can throw the code directly into subscribe
's callback. You also want something like forEach
rather than reduce
this.subexperimentData.subscribe(extractedSResults => {
extractedSResults.forEach(extractedSResult => {
if(extractedSResult.name == this.choosedExperiment) {
extractedSResult.subexperiment.forEach(t => {
this.SubTaskAvg.push(t.avg)
this.SubTaskName.push(t.name)
})
}
})
});