How can I access a variable inside a subscription in an angular pipe to return the transformed value?
What I attempted
transform(value: any, ...args: any[]): any {
const clientKey = args[0];
let arr = [];
let newValue;
this.dbJobs.getJobsFromKey(clientKey).pipe(take(1)).subscribe(jobs => {
if (jobs && jobs.length) {
jobs.forEach((job) => {
arr.push(job.time);
});
}
newValue = arr.reduce((a, b) => {
return a + b;
}, 0);
return newValue;
});
return newValue;
}
In the example above, the newValue
variable is undefined. How can one retrieve it to return the new value for the pipe outside of the Subscription block?