简体   繁体   English

从对象数组创建新的对象数组

[英]Create new Array of Object from Array of Object

Hello guys I want to ask something about create new array of object from array of object.大家好,我想问一些关于从对象数组创建新对象数组的问题。 I need to modify the data because I need to create a grouping bar chart in react-native with victory library.我需要修改数据,因为我需要在 react-native 中使用胜利库创建分组条形图。 They current data is need to be adjusted to create the chart他们当前的数据需要调整以创建图表

the data look like this数据看起来像这样

[
            {
                "date": "April 2022",
                "phase": [
                    {
                        "phaseName": "initiation",
                        "days": 7
                    },
                    {
                        "phaseName": "justification",
                        "days": 6
                    },
                    {
                        "phaseName": "planning",
                        "days": 4
                    }
                ]
            },
            {
                "date": "Mei 2022",
                "phase": [
                    {
                        "phaseName": "justification",
                        "days": 44
                    },
                    {
                        "phaseName": "partner-selection",
                        "days": 32
                    },
                    {
                        "phaseName": "planning",
                        "days": 40
                    },
                    {
                        "phaseName": "initiation",
                        "days": 25
                    },
                    {
                        "phaseName": "signing",
                        "days": 5
                    }
                ]
            },
            {
                "date": "Juni 2022",
                "phase": [
                    {
                        "phaseName": "signing",
                        "days": 6
                    },
                    {
                        "phaseName": "planning",
                        "days": 54
                    },
                    {
                        "phaseName": "justification",
                        "days": 36
                    },
                    {
                        "phaseName": "initiation",
                        "days": 28
                    },
                    {
                        "phaseName": "partner-selection",
                        "days": 30
                    }
                ]
            }
]

how can I create data like this ?我怎样才能创建这样的数据? (data that victory accept) (胜利接受的数据)

[
  {
    x:'April 2022', 
    y: 7, 
    type: 'initiation'
    
  },
  { 
    x: 'Mei 2022', 
    y: 44, 
    type: 'justification' 
    
  }, 
  { 
    x: 'Juni 2022', 
    y: 6, 
    type: 'signing' 
    
  }
],
[
  { 
    x: 'April 2022', 
    y: 6, 
    type: 'justification' 
    
  }, 
  { 
    x: 'Mei 2022', 
    y: 32, 
    type: 'partner-selection' 
    
  }, 
  { 
    x: 'Juni 2022', 
    y: 54, 
    type: 'planning'
  }
],
[
  { 
    x: 'April 2022', 
    y: 4, 
    type: 'planning' 
    
  }, 
  { 
    x: 'Mei 2022',
    y: 40, 
    type: 'planning' 
    
  }, 
  { 
    x: 'Juni 2022', 
    y: 36, 
    type: 'justification' 
    
  }
],
[
  { 
    x: 'Mei 2022', 
    y: 25, 
    type: 'initiation'
  }, 
  { 
    x: 'Juni 2022', 
    y: 28, 
    type: 'initiation'
  }
],
[
  { 
    x: 'Mei 2022', 
    y: 5, 
    type: 'signing'
  }, 
  { 
    x: 'Juni', 
    y: 30, 
    type: 'partner-selection'
      
  }
]

maybe there is a solution with better optimization, but this works and the code is easy to understand :)也许有一个更好的优化解决方案,但这很有效,代码很容易理解:)

 const yourArray = [{"date":"April 2022","phase":[{"phaseName":"initiation","days":7},{"phaseName":"justification","days":6},{"phaseName":"planning","days":4}]},{"date":"Mei 2022","phase":[{"phaseName":"justification","days":44},{"phaseName":"partner-selection","days":32},{"phaseName":"planning","days":40},{"phaseName":"initiation","days":25},{"phaseName":"signing","days":5}]},{"date":"Juni 2022","phase":[{"phaseName":"signing","days":6},{"phaseName":"planning","days":54},{"phaseName":"justification","days":36},{"phaseName":"initiation","days":28},{"phaseName":"partner-selection","days":30}]}]; const res = []; for (const month of yourArray) { for (const phase of month.phase) { res.push({ x: month.date, y: phase.days, type: phase.phaseName }); } }; console.log(res);

You could rename and transpose the items.您可以重命名和转置项目。

 const data = [{ date: "April 2022", phase: [{ phaseName: "initiation", days: 7 }, { phaseName: "justification", days: 6 }, { phaseName: "planning", days: 4 }] }, { date: "Mei 2022", phase: [{ phaseName: "justification", days: 44 }, { phaseName: "partner-selection", days: 32 }, { phaseName: "planning", days: 40 }, { phaseName: "initiation", days: 25 }, { phaseName: "signing", days: 5 }] }, { date: "Juni 2022", phase: [{ phaseName: "signing", days: 6 }, { phaseName: "planning", days: 54 }, { phaseName: "justification", days: 36 }, { phaseName: "initiation", days: 28 }, { phaseName: "partner-selection", days: 30 }] }], result = data.reduce((r, { date: x, phase }) => { phase.forEach(({ phaseName: type, days: y }, i) => { r[i] ??= []; r[i].push({ x, y, type }); }); return r; }, []); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

You can use javascript functional operators like map and reduce like so您可以像这样使用 javascript 函数运算符,例如mapreduce

 const data = [ { date: 'April 2022', phase: [ { phaseName: 'initiation', days: 7, }, { phaseName: 'justification', days: 6, }, { phaseName: 'planning', days: 4, }, ], }, { date: 'Mei 2022', phase: [ { phaseName: 'justification', days: 44, }, { phaseName: 'partner-selection', days: 32, }, { phaseName: 'planning', days: 40, }, { phaseName: 'initiation', days: 25, }, { phaseName: 'signing', days: 5, }, ], }, { date: 'Juni 2022', phase: [ { phaseName: 'signing', days: 6, }, { phaseName: 'planning', days: 54, }, { phaseName: 'justification', days: 36, }, { phaseName: 'initiation', days: 28, }, { phaseName: 'partner-selection', days: 30, }, ], }, ]; const result = data .reduce((array, item) => { array.push( item.phase.map((p) => { return { x: item.date, y: p.days, type: p.phaseName }; }) ); return array; }, []) .reduce( (array, month) => { month.forEach((item, i) => { if (!array[i]) array[i] = []; array[i].push(item); }); return array; }, [[]] ); console.log(result);

Please use this below code请使用下面的代码

const convertedArray = (arr) => {
  let resultantArray = [];

  arr.forEach((item) => {
    const { date } = item;
    const { phase } = item;

    phase.forEach((phaseItem, index) => {
      const { phaseName } = phaseItem;
      const { days } = phaseItem;
      const phaseObject = {
        x: date,
        y: days,
        type: phaseName,
      };
      if (resultantArray.length === 0) {
        resultantArray = [[{ ...phaseObject }]];
      } else {
        if (resultantArray[index]) {
          resultantArray[index] = [...resultantArray[index], phaseObject];
        } else {
          resultantArray[index] = [{ ...phaseObject }];
        }
      }
    });
  });
  return resultantArray;
};

console.log(convertedArray(arr));

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

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