json 以Angular 删除数组中的指定对象

s4n0splo  于 2023-03-13  发布在  Angular
关注(0)|答案(1)|浏览(194)

使用Angular typescript,我得到了一个json数组对象。我想删除我在收到的对象中指定的对象。但是它从来不删除它。

addCategorySub(categorySub: CategorySubModel, index: number) {
  categorySub.id = categorySub.id.toString().trim();

  this.categorySubService.postCategorySub(categorySub)
    .subscribe({
      next: (response: any) => {
        //this.categorySubList.push(response.data);
        //this.categorySubList = [...this.categorySubList];

        this.categoryList[0].categorySubs.push({
          categoryId: response.data.categoryId,
          createdDate: response.data.createdDate,
          id: response.data.id,
          name: response.data.name,
          totalCount: response.data.totalCount,
          updatedDate: response.data.updatedDate
        });
        this.categoryList = [...this.categoryList]
      },
      complete: () => {
        this.messageService.add({severity: 'success', summary: 'Success', detail: `${categorySub.name}.`, life: constants.TOAST_SUCCESS_LIFETIME});
        this.messageService.clear('c');

        //this.categoryList = this.categoryList.filter((p) => p.id !== categorySub.id).filter((p) => p.id !== undefined);
        //this.categoryList[index] = this.categoryList[categorySub.id];
        //delete this.categoryList[categorySub.id]

        //this.categoryList = this.categoryList.filter((p) => p.categorySubs.filter((x) => x.id !== categorySub.id).splice(categorySub.id, 1));

        console.log(this.categoryList)

      },
      error: (e) => {
        this.messageService.add({severity: 'error', summary: 'Hata', detail: `not record \n${e}`, life: constants.TOAST_ERROR_LIFETIME});
        this.messageService.clear('c');
        }
      })
  }

例如,我想删除编号为27.

的数据

export class CategoryModel {
    id: number | any;
    name: string | any;
    companyId: number | any;
    createdDate: Date | undefined;
    updatedDate: Date | undefined;
    totalCount: number | any;
    categorySubs: CategorySubModel[] = []; //any;
  }

  export class CategorySubModel {
    id: number | any;
    name: string | any;
    categoryId: number | any;
    createdDate: Date | undefined;
    updatedDate: Date | undefined;
    totalCount: number | any;
  }

json对象指向categorySubs子对象。所以我想从这里删除
例如下面我有一个json对象,我想删除这个json对象中的一些选中的数据

[
    {
        "id": 3,
        "name": "Yazıcı",
        "businessCode": 0,
        "totalCount": 1,
        "createdDate": "2023-03-01T06:08:13.9752895",
        "updatedDate": "2023-03-13T10:00:42.4496853",
        "companyId": 3,
        "categorySubs": [
            {
                "id": 8,
                "name": "Mobil Yazıcı",
                "createdDate": "2023-03-01T07:17:39.6269764",
                "updatedDate": "2023-03-13T10:00:15.8128449",
                "categoryId": 3
            },
            {
                "id": 9,
                "name": "Lazer Yazıcı3",
                "createdDate": "2023-03-01T07:17:44.0560564",
                "updatedDate": "2023-03-13T10:00:50.2878444",
                "categoryId": 3
            },
            {
                "id": 10,
                "name": "Nokta Vuruşlu Yazıcı",
                "createdDate": "2023-03-01T07:18:01.0370904",
                "updatedDate": "0001-01-01T00:00:00",
                "categoryId": 3
            },
            {
                "id": 4070,
                "name": "asd",
                "createdDate": "2023-03-13T12:59:55.9999323",
                "updatedDate": "0001-01-01T00:00:00",
                "categoryId": 3
            },
            {
                "id": 4071,
                "name": "tert",
                "createdDate": "2023-03-13T13:07:20.3666421",
                "updatedDate": "0001-01-01T00:00:00",
                "categoryId": 3
            },
            {
                "id": "31",
                "name": "wq",
                "companyId": 3,
                "categoryId": 3
            },
            {
                "categoryId": 3,
                "createdDate": "2023-03-13T13:13:43.5022618Z",
                "id": 4072,
                "name": "wq",
                "updatedDate": "0001-01-01T00:00:00"
            }
        ]
    }
  ]
oknwwptz

oknwwptz1#

const item = this.categoryList[0].categorySubs.find(category => category.id === 8);
const index = this.categoryList[0].categorySubs.indexOf(item);
this.categoryList[0].categorySubs.splice(index, 1);

相关问题