Home > Software design >  I want only get the id when filter in angular
I want only get the id when filter in angular

Time:01-10

this my filter in service.components.ts

this.pijatService
        .getById(this.id)
        .pipe(first())
        .subscribe((x: any) => {
          this.form.patchValue(x);
          console.log(x);
          this.form.value.nomor_telepon = x.telepon.nomor_telepon;
          var tempD = x.categories;
          this.form.value.category_id = tempD.filter(
            (data: any) => data.pivot.service_id == this.id
          );
        });

result

categories: Array(2)[
0: {id: 1, name: 'Bayi', slug: 'bayi', created_at: '2021-12-31T08:34:36.000000Z', updated_at: '2021-12-31T08:34:36.000000Z', …}
1: {id: 2, name: 'Capek', slug: 'capek', created_at: '2021-12-31T08:34:36.000000Z', updated_at: '2021-12-31T08:34:36.000000Z', …}
length: 2]

i want final result like this

categories: Array(2)[1,2]

i try to change the filter but its just got me error

another try from answer: let selectedList = x.categories.map((elem: any) => ({ id: elem.id }));

it give me result

0: {id: 1}
1: {id: 2}
length: 2

CodePudding user response:

You can drop other properties this way:

tempD.map(({ id, ...dropAttrs }) => id);

CodePudding user response:

Try this:

let selectedList = categories.map((elem: any) => elem.id);
  •  Tags:  
  • Related