4 votes

Créer un nouveau tableau d'objets à partir d'un tableau d'objets

Bonjour, je voudrais savoir comment créer un nouveau tableau d'objets à partir d'un tableau d'objets. J'ai besoin de modifier les données car j'ai besoin de créer un diagramme de barres groupées en react-native avec la bibliothèque victory. Il faut que les données actuelles soient ajustées pour créer le graphique.

les données se présentent comme suit

[
            {
                "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
                    }
                ]
            }
]

Comment puis-je créer des données comme celles-ci ? (données que la victoire accepte)

[
  {
    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'

  }
]

2voto

AoooR Points 55

Il existe peut-être une solution avec une meilleure optimisation, mais celle-ci fonctionne et le code est facile à comprendre :)

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);

2voto

Nina Scholz Points 17120

Vous pouvez renommer et transposer les éléments.

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; }

1voto

Haris Bouchlis Points 805

Vous pouvez utiliser des opérateurs fonctionnels javascript tels que map y reduce ainsi

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);

0voto

Sanket Tikam Points 61

Veuillez utiliser le code suivant

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));

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X