Skip to content
Advertisement

How to transform one dataset into another recursively?

Given the following data, which I obtain from an api and which is occasionally modified

const data = [
  {
    name: "BG",
    latest: false,
    dir: true,
    etag: null,
    objectName: "BG",
    size: 0,
    lastModified: null,
    storageClass: null,
    versionId: null,
    childItems: [
      {
        etag: '"13dda89d102a62beb58c57a9477b252c"',
        objectName: "BG/item.xml",
        size: 12763,
        lastModified: "2020-11-17T13:13:34.735Z",
        storageClass: "STANDARD",
        versionId: null,
        childItems: null,
        name: "item.xml",
        latest: false,
        dir: false,
      },
      {
        etag: '"5baf1d6bcccc266bfc76cbe36724bd99"',
        objectName: "BG/item.xml",
        size: 17856,
        lastModified: "2020-11-17T13:13:25.796Z",
        storageClass: "STANDARD",
        versionId: null,
        childItems: null,
        name: "item.xml",
        latest: false,
        dir: false,
      },
    ],
  },
  {
    name: "OTHER",
    latest: false,
    dir: true,
    etag: null,
    objectName: "OTHER",
    size: 0,
    lastModified: null,
    storageClass: null,
    versionId: null,
    childItems: [
      {
        etag: '"ab37611f0b217b71804da19d9111bddd"',
        objectName: "OTHER/item.xml",
        size: 3953,
        lastModified: "2020-11-18T09:12:25.634Z",
        storageClass: "STANDARD",
        versionId: null,
        childItems: null,
        name: "item.xml",
        latest: false,
        dir: false,
      },
      {
        name: "MORE",
        latest: false,
        dir: true,
        etag: null,
        objectName: "OTHER/MORE/",
        size: 0,
        lastModified: null,
        storageClass: null,
        versionId: null,
        childItems: [
          {
            etag: '"aea98cd3b3aea692475c329d79c9e7aa"',
            objectName: "OTHER/MORE/item.backup",
            size: 573934,
            lastModified: "2020-11-18T09:45:08.986Z",
            storageClass: "STANDARD",
            versionId: null,
            childItems: null,
            name: "item.backup",
            latest: false,
            dir: false,
          },
          {
            etag: '"1a36b1f644f71a2f62f9c347da5fc381"',
            objectName: "OTHER/MORE/data.txt",
            size: 1045,
            lastModified: "2020-11-18T09:45:08.622Z",
            storageClass: "STANDARD",
            versionId: null,
            childItems: null,
            name: "data.txt",
            latest: false,
            dir: false,
          },
        ],
      },
    ],
  },
];

I need to transform it to the following

[
  {
    label: "BG",
    children: [
      {
        label: "item.xml",
        data: "BG/item.xml",
      },
      {
        label: "item.xml",
        data: "BG/item.xml",
      },
    ],
  },
  {
    label: "OTHER",
    children: [
      {
        label: "item.xml",
        data: "OTHER/item.xml",
      },
      {
        label: "MORE",
        children: [
          {
            label: "item.backup",
            data: "OTHER/MORE/item.backup",
          },
          {
            label: "data.txt",
            data: "OTHER/MORE/data.txt",
          },
        ],
      },
    ],
  },
];

The criterion is that when the dir property of an object is true, obtain the name and assign it to the label property, of the children in the childItem property, obtain the values of the name and objectName properties

It has been a difficult task for me to reason, I thank you in advance for your help

Advertisement

Answer

Iterate over the data items. For each item, get the label and check dir; If true, recurse to process the children, otherwise get the data.

const processItems = items => items.map(item => {
    const results = {
      label: item.name,
    };
    if(item.dir) {
      results.children = processItems(item.childItems);
    }
    else {
      results.data = item.objectName;
    }
    return results;
};

const results = processItems(data);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement