#Create array with just one of the object's data
1 messages · Page 1 of 1 (latest)
What I posted will give you an array like this```ts
[1, 3, 4, 5]
Is that not what you wanted?
it would be something like this ts let cphas_dupli: this.certidao.cphas.map(cert => cert.certDupli_id);
That syntax is not. Probably want this```ts
let cphas_dupli: number[] = this.certidao.cphas.map(cert => cert.certDupli_id);
Post a snippet of your actual data as JSON/JS Object notation if you want my help. 🙂
Your going to have to post more because I don't know the name of your variables
ok
my function: ```ts
duplicadas() {
let cphas_dupli: Array<any> = this.certidao.cphas?.certDupli_id;
console.log(this.certidao.cphas);
this.certService.dupliById(cphas_dupli)
.subscribe({
next: (certDupli) => {
this.certsDupli = certDupli;
}
});
}```
Don't use
it just makes things harder.
You need to assign cphas_dupli inside the next function
and since your variables aren't in english I cannot understand what they mean
return the console.log (4) [{…}, {…}, {…}, {…}] 0 : {id: 1, certidao_id: 2, certDupli_id: 1, created_at: '2022-10-19T14:20:26.000000Z', updated_at: '2022-10-19T14:20:26.000000Z', …} 1 : {id: 2, certidao_id: 2, certDupli_id: 3, created_at: '2022-10-19T14:20:26.000000Z', updated_at: '2022-10-19T14:20:26.000000Z', …} 2 : {id: 3, certidao_id: 2, certDupli_id: 4, created_at: '2022-10-19T14:20:26.000000Z', updated_at: '2022-10-19T14:20:26.000000Z', …} 3 : {id: 4, certidao_id: 2, certDupli_id: 5, created_at: '2022-10-19T14:20:26.000000Z', updated_at: '2022-10-19T14:20:26.000000Z', …} length : 4 [[Prototype]] : Array(0)
That isn't valid JS/JSON
my service ```ts
/**
- Pega os dados da Certidão com duplicidade
- @param certidao ICertidao
- @returns Observable<ICertidao>
*/
dupliById(cphas_dupli: any[]) {
return this.http.post<Array<ICertidao>>(${AppService.apiUrl}/certidao/dupliById, {
cphas_dupli: cphas_dupli,
});
}
this is what returns in my browser console
[
{
"foo": "bar",
"baz": 123,
"qux": false
},
{
"foo": "bar",
"baz": 123,
"qux": false
},
{
"foo": "bar",
"baz": 123,
"qux": false
},
{
"foo": "bar",
"baz": 123,
"qux": false
},
{
"foo": "bar",
"baz": 123,
"qux": false
}
]
[
{
"id": 1,
"certidao_id": 2,
"certDupli_id": 1,
"created_at": "2022-10-19T14:20:26.000000Z",
"updated_at": "2022-10-19T14:20:26.000000Z",
"deleted_at": null
},
{
"id": 2,
"certidao_id": 2,
"certDupli_id": 3,
"created_at": "2022-10-19T14:20:26.000000Z",
"updated_at": "2022-10-19T14:20:26.000000Z",
"deleted_at": null
},
{
"id": 3,
"certidao_id": 2,
"certDupli_id": 4,
"created_at": "2022-10-19T14:20:26.000000Z",
"updated_at": "2022-10-19T14:20:26.000000Z",
"deleted_at": null
},
{
"id": 4,
"certidao_id": 2,
"certDupli_id": 5,
"created_at": "2022-10-19T14:20:26.000000Z",
"updated_at": "2022-10-19T14:20:26.000000Z",
"deleted_at": null
}
]```
And you want to collect all the certDeupli_ids in an array right?
yes! and send to dupliById
this.certService.dupliById(SEND HERE!!)
understand?
Ok. What I posted originally will do that
I don't know the name of the array that holds these objects though so you will have to adjust for that.
It doesn't go because it's in on the return. I need to send the array to return the data I want
You are going to have to post the full code
this.certidao.cphas```this is where objects are returned
dupliIds = this.certidao.cphas.map(cpha => cpha.certDupli_id);
I'm just guessing with the names
but dupliIds will hold ts [1, 3, 4, 5]
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
this array I have to pass HEREts this.certService.dupliById(HERE) not into
Well you have to collect them into an array first right?
I already collected. they here #1032353954895511642 message
You aren't making any sense or aren't reading what I'm saying.
I think you didn't understand me. My English is terrible... I'll try again...
I have a certificate(certidão) table that has a foreign key to cphas_dupli...
the object the console.log(this.certidao) is this ...
inside this return has 'cphas' which is what interests me.
I understand
I need the value of the certDupli_id variables (which are IDs) to fetch the information I want
OK. that is what I posted
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
Now what is next?
With that I want to throw these IDs in an array(cphas_dupli) so that it returns the information and I load my form with what I need
That is what you have an array: #1032353954895511642 message
So how are you supposed to give that array to your service? All at once? One at a time?
duplicadas() {
const cphas_dupli: number[] = this.certidao.cphas.map((item: IcertidaoCphasDouble): number => item.certDupli_id);
this.certService.dupliById(cphas_dupli)
.subscribe({
next: (certDupli) => {
this.certsDupli = certDupli;
}
});
}``` would be something like this
Something like... so what else do you want?
I want this: ```ts
duplicates() {
const cphas_dupli: [1,2,4,5];
this.certService.dupliById(cphas_dupli)
.subscribe({
next: (certDupli) => {
this.certsDupli = certDupli;
}
});
}```
Those are the same thing
Not. don't return
You have a syntax error when you assign cphas_dupli
whats error?
duplicadas() {
const cphas_dupli: number[] = this.certidao.cphas.map((item: IcertidaoCphasDouble): number => item.certDupli_id);
console.log('cphas_dupli', cphas_dupli);
this.certService.dupliById(cphas_dupli).subscribe({
next: (certDupli) => {
this.certsDupli = certDupli;
},
});
}
This:
next: (certDupli) => {
this.certsDupli = certDupli;
},
Happens 1 billion processor instructions after the end of duplicadas()
Because it is Asynchronous Code.
You should not .subscribe to get values out of an Observable in Angular. You should use the AsyncPipe
Show you whole component I guess because I don't know what to tell you to do without knowing where this.certidao comes from nor where this.certsDupli is used
I need .subscribe to load my return information from my search. My code sends the id, takes the information and inserts it into certsDupli
AsyncPipe (| async in your Component template) will handle subscribing and more importantly unsubscribing for you.
Post your code, I can probably suggest something. But if you don't understand this message, then you are going to have a bad time: #1032353954895511642 message
duplicadas() {
const cphas_dupli: number[] = this.certidao.cphas.map((item: IcertidaoCphasDouble): number => item.certDupli_id);
console.log('cphas_dupli', cphas_dupli);
this.certService.dupliById(cphas_dupli).subscribe(
// This part happen much much much later, after the end of this method.
// And because you hid the subscribe inside a method, you have no way to watch and wait for this later code to happen.
);
}
my cod 👆🏿
#1032353954895511642 message my json 'certidao'
my service ```ts
- @param certidao ICertidao
- @returns Observable<ICertidao>
*/
dupliById(cphas_dupli: any[]) {
return this.http.post<Array<ICertidao>>(${AppService.apiUrl}/certidao/dupliById, {
cphas_dupli: cphas_dupli,
});
}```
- @returns Observable<ICertidao>
If you can suggest me something...
import { Component, Input } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { ReplaySubject } from 'rxjs';
import type { Observable } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ICertidao, IcertidaoCphasDouble } from 'src/app/shared/models';
import { CertidoesService } from '../../services';
import { ModalCertComponent } from '../modal-cert/modal-cert.component';
@Component({
selector: 'app-cert-view',
styleUrls: [ './cert-view.component.css' ],
// templateUrl: './cert-view.component.html',
template: `
<pre *ngIf="certsDupli$ | async as certsDupli; else loading">
For Debugging: {{ certsDupli | json }}
</pre>
<ng-template #loading>Loading...</ng-template>
`,
})
export class CertViewComponent {
public readonly certsDupli$: Observable<ICertidao[]>;
private readonly _certidaoSub$: ReplaySubject<ICertidao>;
constructor(
private readonly certService: CertidoesService,
public readonly dialog: MatDialog,
) {
this._certidaoSub$ = new ReplaySubject<ICertidao>(1);
this.certsDupli$ = this._certidaoSub$.pipe(
map((certidao: ICertidao): number[] => certidao.cphas.map((item: IcertidaoCphasDouble): number => item.certDupli_id)),
switchMap((cphas_dupli: number[]): Observable<ICertidao[]> => this.certService.dupliById(cphas_dupli)),
);
}
public openDialog(certidao: ICertidao): void {
this.dialog.open(ModalCertComponent, { data: certidao });
}
// Dados
@Input('certidao') public set certidao(c: ICertidao) {
this._certidaoSub$.next(c);
}
}
IMPO, src/app/shared is an anti-pattern in Angular. So is ../../services. Angular suggests that you group your code into feature modules.
https://angular.io/guide/feature-modules
you could also do:
<pre *ngfor="let certDup of certsDupli$ | async">For Debugging: {{ certDup | json }}</pre>
But I like to have both an *ngIf and a *ngFor so I can do a loading template:
<ul *ngIf="certsDupli$ | async as certsDupli; else loading">
<li *ngFor="let item of certsDupli; trackBy: getCertidaoId">
<pre>For Debugging: {{ item | json }}</pre>
</li>
</ul>
<ng-template #loading>Loading...</ng-template>