简体   繁体   English

基于对象的深层嵌套数组创建一个新对象

[英]create a new object based on deep nested array of objects

Given that I have an object like this鉴于我有这样的对象

const data = {
  _id: "62bac5af6b6581786cb192e4",
  name: "components",
  subCategories: [
    {
      _id: "62bac9a24bd73045dcb563d7",
      name: "RAM",
      subCategories: [
        {
          _id: "62bc21152fde7012505b93a4",
          name: "SRAM",
          subCategories: [],
        },
        {
          _id: "62bc220ff4b1a343a4d2ec9d",
          name: "DRAM",
          subCategories: [],
        },
      ],
    },
    {
      _id: "62baca1a1ffdc142085bca80",
      name: "Motherboard",
      subCategories: [
        {
          _id: "62baca1a1ffdc142085bca82",
          name: "AT",
          subCategories: [],
        },
        {
          _id: "62baca1a1ffdc142085bca83",
          name: "ATX",
          subCategories: [],
        },
      ],
    },
  ],
};

I would like to create a brand new object that would look like this我想创建一个看起来像这样的全新对象

const data = {
    value: "62bac5af6b6581786cb192e4",
    title: "components",
    children: [
      {
        value: "62bac9a24bd73045dcb563d7",
        title: "RAM",
        children: [
          {
            value: "62bc21152fde7012505b93a4",
            title: "SRAM",
            children: [],
          },
          {
            value: "62bc220ff4b1a343a4d2ec9d",
            title: "DRAM",
            children: [],
          },
        ],
      },
      {
        value: "62baca1a1ffdc142085bca80",
        title: "Motherboard",
        children: [
          {
            value: "62baca1a1ffdc142085bca82",
            title: "AT",
            children: [],
          },
          {
            value: "62baca1a1ffdc142085bca83",
            title: "ATX",
            children: [],
          },
        ],
      },
    ],
  };

So my logic was to loop through all the subcategories and insert it as a child.所以我的逻辑是遍历所有子类别并将其作为子类别插入。 Following is what I have tried以下是我尝试过的

const iterate = (data) => {
  let result = {
    title: data.name,
    value: data._id,
    children: [],
  };
  function loop(data, result) {
    for (const category of data) {
      let subItem = {
        title: category.name,
        value: category._id,
        children: [],
      };
      result.children.push(subItem);
      if (category.subCategories) {
        loop(category.subCategories, result);
      }
    }
  }
  loop(data.subCategories, result);
  return result;
};

const res = iterate(data);
console.log(res);

I end up getting something like this我最终得到了这样的东西

{
    "title": "components",
    "value": "62bac5af6b6581786cb192e4",
    "children": [
        {
            "title": "RAM",
            "value": "62bac9a24bd73045dcb563d7",
            "children": []
        },
        {
            "title": "SRAM",
            "value": "62bc21152fde7012505b93a4",
            "children": []
        },
        {
            "title": "DRAM",
            "value": "62bc220ff4b1a343a4d2ec9d",
            "children": []
        },
        {
            "title": "Motherboard",
            "value": "62baca1a1ffdc142085bca80",
            "children": []
        },
        {
            "title": "AT",
            "value": "62baca1a1ffdc142085bca82",
            "children": []
        },
        {
            "title": "ATX",
            "value": "62baca1a1ffdc142085bca83",
            "children": []
        }
    ]
}

Kinda stuck here, really appreciate all the help given, thank you.有点卡在这里,非常感谢提供的所有帮助,谢谢。

You can try this:你可以试试这个:

const transform = (data) => {
    const result = {
        title: data.name,
        value: data._id,
        children: data.subCategories.map(transform),
    };
    return result;
}
const newData = transform(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM