我如何递归地迭代对象数组并获得一个带有过滤数据的新数组作为结果

问题描述 投票:0回答:1
[
      {
      "id": "11ed-a261-0242ac120002",
      "name": "Languages",
      "code": 1,
      "description": "Here you can find teacher for any language. Learn when you want and how you want.",
      "childrenCategories": [
          {
          "id": "a261-0242ac120002",
          "name": "German",
          "code": 7,
          "description": "Here you can find teacher for German. Learn when you want and how you want.",
          "childrenCategories": []
          },
          {
          "id": "a261-0242ac120002",
          "name": "Spanish",
          "code": 8,
          "description": "Here you can find teacher for Spanish. Learn when you want and how you want.",
          "childrenCategories": []
          },
          ]
      },
      {
      "id": "a261-0242ac120002",
      "name": "Sciences",
      "code": 3,
      "description": "It is your online university. Learn whatever you want and become better.",
      "childrenCategories": [
          {
          "id": "6a66915a-3439-11ed-a261-0242ac120002",
          "name": "Mathematics",
          "code": 12,
          "description": "It is your online university. Learn Mathematics and become better.",
          "childrenCategories": []
          },
          {
          "id": "6a669420-3439-11ed-a261-0242ac120002",
          "name": "Physics",
          "code": 13,
          "description": "It is your online university. Learn Physics and become better.",
          "childrenCategories": []
          },
      ]
      },
  ]

我想要得到这样一个数组

[
    {"name": "Languages", "code": 1},
    {"name": "German", "code": 7},
    {"name": "Spanish", "code": 8},
    {"name": "Sciences", "code": 3},
    {"name": "Mathematics", "code": 12},
    {"name": "Physics", "code": 13,},
]

我有一个包含嵌套对象和数组的数组,我想递归过滤并返回一个不嵌套的新数组,该数组仅包含字段“name”:“Languages”“code”:1, 我写了下面的代码。他工作。我想知道解决这个问题的其他选择。

const flatten = (arr) => {
        let result = []

        arr.flatMap((item) => {
            result.push({name: item.name, code: item.code})
            item.childrenCategories.map(child =>
                result.push({name: child.name, code: child.code})
            )
        })
        return result
    }
javascript reactjs methods iteration
1个回答
0
投票

这是使用递归解决此问题的另一种方法:

function flattenCategories(categories) {
  let result = [];

  function flatten(category) {
    const { name, code, childrenCategories } = category;
    result.push({ name, code });

    if (childrenCategories && childrenCategories.length > 0) {
      childrenCategories.forEach(child => flatten(child));
    }
  }

  categories.forEach(category => flatten(category));

  return result;
}

const data = [
  {
    "id": "11ed-a261-0242ac120002",
    "name": "Languages",
    "code": 1,
    "description": "Here you can find teacher for any language. Learn when you want and how you want.",
    "childrenCategories": [
      {
        "id": "a261-0242ac120002",
        "name": "German",
        "code": 7,
        "description": "Here you can find teacher for German. Learn when you want and how you want.",
        "childrenCategories": []
      },
      {
        "id": "a261-0242ac120002",
        "name": "Spanish",
        "code": 8,
        "description": "Here you can find teacher for Spanish. Learn when you want and how you want.",
        "childrenCategories": []
      },
    ]
  },
  {
    "id": "a261-0242ac120002",
    "name": "Sciences",
    "code": 3,
    "description": "It is your online university. Learn whatever you want and become better.",
    "childrenCategories": [
      {
        "id": "6a66915a-3439-11ed-a261-0242ac120002",
        "name": "Mathematics",
        "code": 12,
        "description": "It is your online university. Learn Mathematics and become better.",
        "childrenCategories": []
      },
      {
        "id": "6a669420-3439-11ed-a261-0242ac120002",
        "name": "Physics",
        "code": 13,
        "description": "It is your online university. Learn Physics and become better.",
        "childrenCategories": []
      },
    ]
  },
];

const result = flattenCategories(data);
console.log(result);

© www.soinside.com 2019 - 2024. All rights reserved.