首页 > 解决方案 > Groupby 对象数组多次

问题描述

考虑以下数据

let dataset =
    [
      {
        "Category": "Auto",
        "Parent": "Automotive",
        "name": "Auto - Multi Purpose Vehicles",
        "High": "10",
        "Low": "15",
        "Medium": "5"
      },
      {
        "Category": "Auto",
        "Parent": "Automotive",
        "name": "Auto - Off road",
        "High": "1",
        "Low": "2",
        "Medium": "3"
      },
      {
        "Category": "Auto",
        "Parent": "Ecology & Environment",
        "name": "Vegetarian",
        "High": "5",
        "Low": "5",
        "Medium": "10"
      },
      {
        "Category": "Home",
        "Parent": "Ecology & Environment",
        "name": "Weather",
        "High": "10",
        "Low": "15",
        "Medium": "5"
      }]

我想按以下方式对数据进行分组

[{
  name: "Auto",
  data: [{
            name: "Automotive",
        data: [{
                name: "Auto - Multi Purpose Vehicles",
            value: 30
                High: 10,
                Low: 15,
                Medium: 5
            },
           {
                name: "Auto - Off road",
            value: 6
                High: 1,
                Low: 2,
                Medium: 3
          }]
        }, {
        name: 'Ecology & Environment',
        data: [{
          name: "Vegetarian",
           value: 20
               High: 5,
               Low: 5,
               Medium: 10
          }]
        }],
  name: "Home",
  data: [{
        name: "Ecology & Environment",
        data: [{
            name: "Weather",
            value: 30
            High: 10,
            Low: 15,
            Medium: 5
            }]
          }]

}]

到目前为止,我尝试过的东西从来没有让我达到理想的输出。

这是我堆叠的地方。

let cat = []
        
        dataset.forEach((d) => {
          const { Category, Parent, name, High, Medium, Low, value } = d;
          let category = cat.find((item) => item.Category === Category);
          let parent = cat.find((item) => item.data.Parent === Parent);
        
          if (!category) {
            cat.push({
              Category,
              data: []
            })
          } else if (!parent ) {
           
          cat.forEach(i => {
            i.data.push({
              Parent,
              High,
              Medium,
              Low
            })
          })
          }
        })

我已经尝试了几种不同结果的方法,但我无法弄清楚如何处理它我不是 javascript 专家。我正在尝试边做边学,但有时我会得到堆栈非常感谢P的任何帮助。

标签: arraysobjectnested

解决方案


为了获得您想要的答案,请遵循此代码。

第 1 步: 从数据集中获取唯一类别

第二步: 得到一个唯一的类别后,在数据集中寻找特定的类别,找到一个类别后将其推入数组

按照下面的代码片段

let dataset = [
  {
    Category: "Auto",
    Parent: "Automotive",
    name: "Auto - Multi Purpose Vehicles",
    High: "10",
    Low: "15",
    Medium: "5",
  },
  {
    Category: "Auto",
    Parent: "Automotive",
    name: "Auto - Off road",
    High: "1",
    Low: "2",
    Medium: "3",
  },
  {
    Category: "Auto",
    Parent: "Ecology & Environment",
    name: "Vegetarian",
    High: "5",
    Low: "5",
    Medium: "10",
  },
  {
    Category: "Home",
    Parent: "Ecology & Environment",
    name: "Weather",
    High: "10",
    Low: "15",
    Medium: "5",
  },
];

/* map function help us to get non unique 
  categoty like => [ 'Auto', 'Auto', 'Auto', 'Home' ] 
  `New Set()` object gave us unique array then
  we create new array using spread oprator */
let uniqueCategory = [...new Set(dataset.map((data) => data.Category))];
let uniqueParent = [...new Set(dataset.map((data) => data.Parent))];

// create new array for storing final output
let cat = [];

// loop over unique category
uniqueCategory.forEach((Category) => {
  // create new object for perticular category
  let obj = {
    name: Category,
    data: [],
  };

  uniqueParent.forEach((parent) => {
    let isDataEmpty = true;
    let parentObj = {
      name: parent,
      data: [],
    };
    // loop over dataset
    dataset.map((data) => {
      /* if we fount category same as unique category we have
              to push that data into particular category object */
      if (data.Category === Category && data.Parent === parent) {
        // delete category ket/value from object
        isDataEmpty = false;
        delete data.Category;
        parentObj.data.push(data);
      }
    });
    if (!isDataEmpty) obj.data.push(parentObj);
  });
  cat.push(obj);
});

console.log(cat);


推荐阅读