本文介绍了将父级ID信息添加到层次结构中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取上一个对象的所有ID值,并在每个父项中构建一个ID数组。这需要以通用方式完成,并且不能使用属性的名称。但是,每个属性的确会继承基类。结构上可能存在其他一些数组,这些数组不是从 SubResource 类继承的,因此只有那些数组可以添加到identifierHierarchy中。

I need to grab all of the ID values from the previous object and build out an array of the IDs in each parent item. This needs to be done in a generic manner and cannot use the name of the property. However, each property does inherit a base class. There may be other arrays on the structure that do not inherit from the SubResource class, so only those that do, should be added to the identifierHierarchy.

export abstract class SubResource {
  public id: number;
  public identifierHierarchy: number[] = [];
}

给出以下数据段

let data = [{
  "id": "1",
  "name": "Deer, spotted",
  "parents": [
    {
      "id": "133",
      "name": "Jaime Coldrick",
      "children": [
        {
          "id": "0723",
          "name": "Ardys Kurten",
          "grandchildren": [
            {
              "id": "384",
              "name": "Madelle Bauman"
            },
            {
              "id": "0576",
              "name": "Pincas Maas"
            },
            {
              "id": "5",
              "name": "Corrie Beacock"
            }
          ]
        }]
    }]
}]

我现在需要对象上的值。

I need for the values on the objects to now be.

[{
    "id": "1",
    "name": "Deer, spotted",
    "parents": [{
        "id": "133",
        "name": "Jaime Coldrick",
        "identifierHierarchy": ["1"],
        "children": [{
            "id": "0723",
            "name": "Ardys Kurten",
            "identifierHierarchy": ["1", "133"],
            "grandchildren": [{
                    "id": "384",
                    "name": "Madelle Bauman",
                    "identifierHierarchy": ["1", "133", "0723"]
                },
                {
                    "id": "0576",
                    "name": "Pincas Maas",
                    "identifierHierarchy": ["1", "133", "0723"]
                },
                {
                    "id": "5",
                    "name": "Corrie Beacock",
                    "identifierHierarchy": ["1", "133", "0723"]
                }
            ]
        }]
    }]
}]


推荐答案

不是类型安全的e如我所愿,但是...

Not as type-safe as I would like, but...

export class RestHierarchyService {
  public static assignHierarchyIdentifiers(subResource: SubResource | any, parentIdentifiers: any): void {
    const composedSubResources = Object.entries(subResource)
      .filter(([key, value]) => key !== 'identifierHierarchy' && value.constructor === Array)
      .map(([key, value]) => Object.values(value).reduce(x => x));

    subResource.identifierHierarchy = parentIdentifiers;
    composedSubResources.forEach(x => RestHierarchyService.assignHierarchyIdentifiers(x, [...parentIdentifiers, subResource.id]));
  }
}



ancestry.forEach(x => x.parents.forEach(y => RestHierarchyService.assignHierarchyIdentifiers(y, [x.id])));

这篇关于将父级ID信息添加到层次结构中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 14:50