Home > Blockchain >  Formatt array of obj
Formatt array of obj

Time:11-05

i have this array of obj

  const pressure = [
  {
    date: "2021-11-03T23:51:55.875Z",
    diastolica: 72,
    pulsazione: 69,
    sistolica: 130,
    user: "61830313ba36bf2504df0ec3",
    __v: 0,
    _id: "6183209bf91a7ed54a76c05e",
  },
  {
    date: "2021-11-03T23:52:09.684Z",
    diastolica: 75,
    pulsazione: 71,
    sistolica: 135,
    user: "61830313ba36bf2504df0ec3",
    __v: 0,
    _id: "618320a9f91a7ed54a76c061",
  },
];

What I would like to get is an array of objects formatted like this

[
      {
        name: "Sistolica",
        data: [130,135],
      },
      {
        name: "Diastolica",
        data: [72,75]
      },
      {
        name: "Pulsazione",
        data: [69,71],
      },
    ],

For use within apex charts The solution I found is not suitable and above all it is not reusable, if I passed an array that does not have the same keys that I indicated in my helper function, everything would be for the worse. Can anyone help me with this? I post the solution I had adopted, but I know it's really awful

export const setupGraphSeries = (data) => {
 
  const sistolica = [];
  const diastolica = [];
  const pulsazione = [];

  const formatter = data.map((item) => {
    sistolica.push(item["sistolica"]);
    diastolica.push(item["diastolica"]);
    pulsazione.push(item["pulsazione"]);
    return [
      { name: "Sistolica", data: sistolica },
      { name: "Diastolica", data: diastolica },
      { name: "Pulsazione", data: pulsazione },
    ];
  });

  return formatter;
};

CodePudding user response:

One way is to just map over an array of the required properties. This does mean mapping over pressures once per item:

const pressure = [
  {
    date: "2021-11-03T23:51:55.875Z",
    diastolica: 72,
    pulsazione: 69,
    sistolica: 130,
    user: "61830313ba36bf2504df0ec3",
    __v: 0,
    _id: "6183209bf91a7ed54a76c05e",
  },
  {
    date: "2021-11-03T23:52:09.684Z",
    diastolica: 75,
    pulsazione: 71,
    sistolica: 135,
    user: "61830313ba36bf2504df0ec3",
    __v: 0,
    _id: "618320a9f91a7ed54a76c061",
  },
];

const params = ["diastolica","pulsazione","sistolica"];

const result = params.map( name => ({
   name,
   data: pressure.map(x => x[name])
}));

console.log(result);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Another way is to reduce the original keeping track of whether you have that item yet. This doesnt require multiple passes over the original data but is a little more complex:

const pressure = [
  {
    date: "2021-11-03T23:51:55.875Z",
    diastolica: 72,
    pulsazione: 69,
    sistolica: 130,
    user: "61830313ba36bf2504df0ec3",
    __v: 0,
    _id: "6183209bf91a7ed54a76c05e",
  },
  {
    date: "2021-11-03T23:52:09.684Z",
    diastolica: 75,
    pulsazione: 71,
    sistolica: 135,
    user: "61830313ba36bf2504df0ec3",
    __v: 0,
    _id: "618320a9f91a7ed54a76c061",
  },
];

const params = ["diastolica","pulsazione","sistolica"];

const result = Object.values(pressure.reduce( (a,item) => {
    for(var i=0;i<params.length;i  ){
       const name = params[i];
       a[name] = a[name] || {name,data:[]}
          
      a[name].data.push(item[name]);
    }
    return a;
},{}));

console.log(result);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related