#Why is this undefined?
34 messages · Page 1 of 1 (latest)
Why do you use any? Stop using any. Use correct types. You're shooting yourself in the foot by using any: you're implementing plenty of nonsensical stuff just because you use any and the compiler can't tell you you're doing something nonsensical.
What is this code supposed to do. What is this.mockArray$? What is undefined and shouldn't be, and why?
That is an observable of number[]
I just wanted to find the element that is above 6 and console log it.
public mockArrayObservable(): Observable<number[]> {
this.mockArray$ = of([1, 2, 4, 5, 6, 7, 8, 9]);
return this.mockArray$;
}
//Obser
If it's an observable of number[], each element it emits is a number[], i.e. an array of numbers. How could an array of numbers be greater than 6? That doesn't make sense.
Use the correct type: Observable<number[]>, and you'll have an error from the comoiler:
Operator '>' cannot be applied to types 'number[]' and 'number'.
Please use code block. Not pictures. I'm sure we told you that a dozen times already, just as do not use any. So, start by rewriting that code without, ever, using any. Then, if you're still stuck, post it a text, using a code block, as shown below:
g!codeblock @gusty fjord
@gusty fjord, you can use the following snippet to have your code formatted and colored by Discord. Replace ts with the language you need (i.e. html, js, css, etc)
```ts
// your code goes here
```
this.findResult = this.mockArray$.pipe(map(x => x.find((element: any) => element > 6)));
this.findResult.subscribe((x: any) => {
console.log(x + "test")
})
That's a completely different piece of code. What's your question about it?
Sorry fixed.
It's still a different piece of code, you still didn't ask a question, and you didn't fix it: you made it worse by, once again, using any.
Yeah I added the map
Okay so if mockArray is an Observable<number[]>
How is this suppose to be written?
To use find and get the first element above 6
then console log it
We firstly have to map into the array right?
start by rewriting that code without, ever, using any
On it
Lol worked
Why did literally changing that work
this.findResult = this.mockArray$.pipe(map(x => x.find((element: number) => element > 6)));
this.findResult.subscribe((x: number) => {
console.log(x + "test")
})
Wait it worked with any too.
My map must be working now.
Your code now uses the map RxJS operator to transform the arrays of numbers that the observable emits into numbers. Your original code used the RxKS find() operator to create on abservable emitting the first array that is greater than 6, which makes no sense at all. And you would have known, from the compiler, that you has done something nonsensical if you hadn't used any. Do not use any.
How is this,I even added the x type
//RxJs find from number[]
this.findResult = this.mockArray$.pipe(map((x:number[]) => x.find((element: number) => element > 6)));
this.findResult.subscribe((x: number) => {
console.log(x + " test")
})```
Which helps to truly understand what is going on.
Are you asking a question here? I don't know what you're asking.