Voici le code:

rowData = [
{
            server: "Server 1",
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 09:00:00",
            dateEnd: "2019-10-12 09:05:000"
        },{
            server: "Server 1",
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 08:40:00",
            dateEnd: "2019-10-12 08:45:000"
        },{
            server: "Server 1",
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 08:15:00",
            dateEnd: "2019-10-12 08:25:000"
        },{
            server: "Server 1",
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 08:10:00",
            dateEnd: "2019-10-12 08:15:000"
        },{
            server: "Server 1",
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 08:00:00",
            dateEnd: "2019-10-12 08:05:000"
        },{
            server: "Server 1",
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 07:00:00",
            dateEnd: "2019-10-12 08:05:000"
        },{
            server: "Server 1",
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 06:00:00",
            dateEnd: "2019-10-12 07:05:000"
        },{
            server: "Server 1",
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 05:00:00",
            dateEnd: "2019-10-12 06:05:000"
        },{
            server: "Server 1",
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 04:00:00",
            dateEnd: "2019-10-12 05:05:000"
        }

{
            server: "Server 2",
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 09:03:00",
            dateEnd: "2019-10-12 09:05:000"
            },
            {
            server: "Server 2",
            ping: "2 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 09:00:00",
            dateEnd: "2019-10-12 09:01:000"
            }
}

Ce que je veux faire, c'est regrouper les données en fonction du serveur qui est le "serveur 1", puis il obtiendra les dernières données. cela dépend si elle sera basée sur la date qui est le dateStart et dateEnd.

La sortie devrait être comme ceci:

[
    {
        server: "Server 1",
        ping: "10 ms",
        dl: "50Mbit/s",
        ul: "50Mbit/s",
        ispcon: true,
        dateStart: "2019-10-12 09:00:00",
        dateEnd: "2019-10-12 09:05:000"
        data: [
        {
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 09:00:00",
            dateEnd: "2019-10-12 09:05:000"
        },{
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 08:40:00",
            dateEnd: "2019-10-12 08:45:000"
        },{
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 08:15:00",
            dateEnd: "2019-10-12 08:25:000"
        },{
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 08:10:00",
            dateEnd: "2019-10-12 08:15:000"
        },{
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 08:00:00",
            dateEnd: "2019-10-12 08:05:000"
        },{
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 07:00:00",
            dateEnd: "2019-10-12 08:05:000"
        },{
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 06:00:00",
            dateEnd: "2019-10-12 07:05:000"
        },{
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 05:00:00",
            dateEnd: "2019-10-12 06:05:000"
        },{
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 04:00:00",
            dateEnd: "2019-10-12 05:05:000"
        }
        ]
        },
        {
        server: "Server 2",
        ping: "10 ms",
        dl: "50Mbit/s",
        ul: "50Mbit/s",
        ispcon: true,
        dateStart: "2019-10-12 09:03:00",
        dateEnd: "2019-10-12 09:05:000"
        data: [
        {
            ping: "10 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 09:03:00",
            dateEnd: "2019-10-12 09:05:000"
            },
            {
            ping: "2 ms",
            dl: "50Mbit/s",
            ul: "50Mbit/s",
            ispcon: true,
            dateStart: "2019-10-12 09:00:00",
            dateEnd: "2019-10-12 09:01:000"
            }
        ]
    }
]

Merci d'avance

0
ABC 14 janv. 2020 à 07:42

2 réponses

Meilleure réponse

Utilisez la méthode reduce et combinez avec Object.values. Voici un exemple de code sur une ligne.

const rowData = [
  {
    server: "Server 1",
    ping: "10 ms",
    dl: "50Mbit/s",
    ul: "50Mbit/s",
    ispcon: true,
    dateStart: "2019-10-12 09:00:00",
    dateEnd: "2019-10-12 09:05:000"
  },
  {
    server: "Server 1",
    ping: "10 ms",
    dl: "50Mbit/s",
    ul: "50Mbit/s",
    ispcon: true,
    dateStart: "2019-10-12 08:40:00",
    dateEnd: "2019-10-12 08:45:000"
  },
  {
    server: "Server 1",
    ping: "10 ms",
    dl: "50Mbit/s",
    ul: "50Mbit/s",
    ispcon: true,
    dateStart: "2019-10-12 08:15:00",
    dateEnd: "2019-10-12 08:25:000"
  },
  {
    server: "Server 1",
    ping: "10 ms",
    dl: "50Mbit/s",
    ul: "50Mbit/s",
    ispcon: true,
    dateStart: "2019-10-12 08:10:00",
    dateEnd: "2019-10-12 08:15:000"
  },
  {
    server: "Server 1",
    ping: "10 ms",
    dl: "50Mbit/s",
    ul: "50Mbit/s",
    ispcon: true,
    dateStart: "2019-10-12 08:00:00",
    dateEnd: "2019-10-12 08:05:000"
  },
  {
    server: "Server 1",
    ping: "10 ms",
    dl: "50Mbit/s",
    ul: "50Mbit/s",
    ispcon: true,
    dateStart: "2019-10-12 07:00:00",
    dateEnd: "2019-10-12 08:05:000"
  },
  {
    server: "Server 1",
    ping: "10 ms",
    dl: "50Mbit/s",
    ul: "50Mbit/s",
    ispcon: true,
    dateStart: "2019-10-12 06:00:00",
    dateEnd: "2019-10-12 07:05:000"
  },
  {
    server: "Server 1",
    ping: "10 ms",
    dl: "50Mbit/s",
    ul: "50Mbit/s",
    ispcon: true,
    dateStart: "2019-10-12 05:00:00",
    dateEnd: "2019-10-12 06:05:000"
  },
  {
    server: "Server 1",
    ping: "10 ms",
    dl: "50Mbit/s",
    ul: "50Mbit/s",
    ispcon: true,
    dateStart: "2019-10-12 04:00:00",
    dateEnd: "2019-10-12 05:05:000"
  },

  {
    server: "Server 2",
    ping: "10 ms",
    dl: "50Mbit/s",
    ul: "50Mbit/s",
    ispcon: true,
    dateStart: "2019-10-12 09:03:00",
    dateEnd: "2019-10-12 09:05:000"
  },
  {
    server: "Server 2",
    ping: "2 ms",
    dl: "50Mbit/s",
    ul: "50Mbit/s",
    ispcon: true,
    dateStart: "2019-10-12 09:00:00",
    dateEnd: "2019-10-12 09:01:000"
  }
];

const res = Object.values(
  rowData.sort((a, b) => 
     (new Date(a.dateStart).getTime()) - 
       (new Date(b.dateStart).getTime())).reduce((acc, curr) => {
    const { server, ...rest_curr } = curr;
    return {
      ...acc,
      [curr.server]:
        curr.server in acc
          ? {
              ...acc[curr.server],
              data: [...acc[curr.server].data, { ...rest_curr }]
            }
          : { ...curr, data: [{ ...rest_curr }] }
    };
  }, {})
);

console.log(res);
2
Siva Kondapi Venkata 20 janv. 2020 à 04:50

J'ai un peu simplifié votre objet retourné, donc les propriétés de premier niveau sont simplement server et data, et server est supprimé des objets de deuxième niveau.

let arr=rowData=[{server:"Server 1",ping:"10 ms",dl:"50Mbit/s",ul:"50Mbit/s",ispcon:!0,dateStart:"2019-10-12 09:00:00",dateEnd:"2019-10-12 09:05:000"},{server:"Server 1",ping:"10 ms",dl:"50Mbit/s",ul:"50Mbit/s",ispcon:!0,dateStart:"2019-10-12 08:40:00",dateEnd:"2019-10-12 08:45:000"},{server:"Server 1",ping:"10 ms",dl:"50Mbit/s",ul:"50Mbit/s",ispcon:!0,dateStart:"2019-10-12 08:15:00",dateEnd:"2019-10-12 08:25:000"},{server:"Server 1",ping:"10 ms",dl:"50Mbit/s",ul:"50Mbit/s",ispcon:!0,dateStart:"2019-10-12 08:10:00",dateEnd:"2019-10-12 08:15:000"},{server:"Server 1",ping:"10 ms",dl:"50Mbit/s",ul:"50Mbit/s",ispcon:!0,dateStart:"2019-10-12 08:00:00",dateEnd:"2019-10-12 08:05:000"},{server:"Server 1",ping:"10 ms",dl:"50Mbit/s",ul:"50Mbit/s",ispcon:!0,dateStart:"2019-10-12 07:00:00",dateEnd:"2019-10-12 08:05:000"},{server:"Server 1",ping:"10 ms",dl:"50Mbit/s",ul:"50Mbit/s",ispcon:!0,dateStart:"2019-10-12 06:00:00",dateEnd:"2019-10-12 07:05:000"},{server:"Server 1",ping:"10 ms",dl:"50Mbit/s",ul:"50Mbit/s",ispcon:!0,dateStart:"2019-10-12 05:00:00",dateEnd:"2019-10-12 06:05:000"},{server:"Server 1",ping:"10 ms",dl:"50Mbit/s",ul:"50Mbit/s",ispcon:!0,dateStart:"2019-10-12 04:00:00",dateEnd:"2019-10-12 05:05:000"},{server:"Server 2",ping:"10 ms",dl:"50Mbit/s",ul:"50Mbit/s",ispcon:!0,dateStart:"2019-10-12 09:03:00",dateEnd:"2019-10-12 09:05:000"},{server:"Server 2",ping:"2 ms",dl:"50Mbit/s",ul:"50Mbit/s",ispcon:!0,dateStart:"2019-10-12 09:00:00",dateEnd:"2019-10-12 09:01:000"}];

let res = rowData.reduce((acc,cur) => {
    if(acc.some(el => el.server && el.server === cur.server)){
        return acc.map(el => {
            if(el.server === cur.server){
                delete cur.server
                el.data.push(cur) 
            }            
            return el
        })
    }else{
        let server = cur.server
        delete cur.server
        return acc.concat({server: server, data: [cur]})
    }
    
},[])

console.log(res)
1
symlink 14 janv. 2020 à 05:17