#Create array with just one of the object's data

1 messages · Page 1 of 1 (latest)

dusk canyon
#
certDupliIds = this.certsDupli.map(cert => cert.certDupli_id);
#

What I posted will give you an array like this```ts
[1, 3, 4, 5]

Is that not what you wanted?
analog oracle
#

it would be something like this ts let cphas_dupli: this.certidao.cphas.map(cert => cert.certDupli_id);

dusk canyon
#

That syntax is not. Probably want this```ts
let cphas_dupli: number[] = this.certidao.cphas.map(cert => cert.certDupli_id);

rose oriole
#

Post a snippet of your actual data as JSON/JS Object notation if you want my help. 🙂

dusk canyon
#

Your going to have to post more because I don't know the name of your variables

analog oracle
#

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;
    }
  });

}```

rose oriole
#

Don't use any it just makes things harder.

dusk canyon
#

You need to assign cphas_dupli inside the next function

rose oriole
#

and since your variables aren't in english I cannot understand what they mean

analog oracle
#

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)

analog oracle
#

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,
    });
    }
analog oracle
rose oriole
#
[
  {
    "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
  }
]
analog oracle
# rose oriole ```json [ { "foo": "bar", "baz": 123, "qux": false }, { "f...
[
    {
        "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
    }
]```
dusk canyon
#

And you want to collect all the certDeupli_ids in an array right?

analog oracle
#

this.certService.dupliById(SEND HERE!!)

#

understand?

dusk canyon
#

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.

analog oracle
dusk canyon
#

You are going to have to post the full code

analog oracle
dusk canyon
#

I'm just guessing with the names

#

but dupliIds will hold ts [1, 3, 4, 5]

analog oracle
dusk canyon
analog oracle
dusk canyon
#

You aren't making any sense or aren't reading what I'm saying.

analog oracle
#

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.

dusk canyon
#

I understand

analog oracle
#

I need the value of the certDupli_id variables (which are IDs) to fetch the information I want

dusk canyon
#

OK. that is what I posted

analog oracle
#

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

rose oriole
#

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?

analog oracle
rose oriole
#

Something like... so what else do you want?

analog oracle
rose oriole
#

Those are the same thing

analog oracle
#

Not. don't return

dusk canyon
#

You have a syntax error when you assign cphas_dupli

analog oracle
rose oriole
#
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

analog oracle
#

I need .subscribe to load my return information from my search. My code sends the id, takes the information and inserts it into certsDupli

rose oriole
#

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.
  );
}
analog oracle
#

#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,
      });
      }```
#

If you can suggest me something...

rose oriole
#
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);
  }
}
#

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>