2017-10-18 26 views
0

J'ai un tableau d'objets qui doit être combiné en fonction de chaque heure. Par exemple:
Ségrégation d'un tableau d'objets en fonction de l'attribut horodateur

[{"id": "12345", "data": "abc", "timestamp": "2017-10-17T00:05:30.523Z"}, 
{"id": "16375", "data": "sgr", "timestamp": "2017-10-17T00:23:54.234Z"}, 
{"id": "46537", "data": "etd", "timestamp": "2017-10-17T01:36:16.463Z"}, 
{"id": "83645", "data": "eth", "timestamp": "2017-10-17T01:32:25.640Z"}, 
{"id": "36153", "data": "her", "timestamp": "2017-10-17T01:56:13.478Z"}, 
{"id": "31383", "data": "sry", "timestamp": "2017-10-17T05:56:56.362Z"}, 
{"id": "68123", "data": "rya", "timestamp": "2017-10-17T06:34:30.652Z"}] 

Je veux que les données résultantes dans le format suivant:

{ "00:00-00:59": 
[{"id": "12345", "data": "abc", "timestamp": "2017-10-17T00:05:30.523Z"}, 
{"id": "16375", "data": "sgr", "timestamp": "2017-10-17T00:23:54.234Z"}] 
"01:00-01:59": 
[{"id": "46537", "data": "etd", "timestamp": "2017-10-17T01:36:16.463Z"}, 
{"id": "83645", "data": "eth", "timestamp": "2017-10-17T01:32:25.640Z"}, 
{"id": "36153", "data": "her", "timestamp": "2017-10-17T01:56:13.478Z"}] 
"05:00-05:59": [{"id": "31383", "data": "sry", "timestamp": "2017-10-17T05:56:56.362Z"}] 
"06:00-06:59": [{"id": "68123", "data": "rya", "timestamp": "2017-10-17T06:34:30.652Z"}] 

Est-il possible de formater les données mentionnées ci-dessus? Comment puis-je écrire un code court pour l'exigence?

Répondre

2

essayer ce code

var a = [{"id": "12345", "data": "abc", "timestamp": "2017-10- 17T00:05:30.523Z"}, 
{"id": "16375", "data": "sgr", "timestamp": "2017-10-17T00:23:54.234Z"}, 
{"id": "46537", "data": "etd", "timestamp": "2017-10-17T01:36:16.463Z"}, 
{"id": "83645", "data": "eth", "timestamp": "2017-10-17T01:32:25.640Z"}, 
{"id": "36153", "data": "her", "timestamp": "2017-10-17T01:56:13.478Z"}, 
{"id": "31383", "data": "sry", "timestamp": "2017-10-17T05:56:56.362Z"}, 
{"id": "68123", "data": "rya", "timestamp": "2017-10-17T06:34:30.652Z"}] 

var t, h, n, obj = {}; 
for(var i=0; i<a.length; i++) { 
    t = new Date(a[i].timestamp); 

    if (!isNaN(t.getTime())) { //if date is valid 
     h = t.getHours(); 
     n = h + ':00-' + h + ':59'; 
     if(typeof obj[n] === 'undefined') obj[n] = []; 
     obj[n].push(a[i]); 
    } 
} 

console.log(obj); 
+0

Cela fonctionne, il suffit de mettre un constructeur après vérification pour voir si 't' est une date valide. – gurvinder372

+0

@ Firemen26 merci beaucoup. a travaillé très bien – Chetanraj

+0

wow. C'est génial! – levi

0

Vous pouvez utiliser la méthode Array.reduce pour convertir votre tableau dans un objet désirable.

Voir l'exemple ci-dessous.

var a = [{"id": "12345", "data": "abc", "timestamp": "2017-10- 17T00:05:30.523Z"}, 
 
{"id": "16375", "data": "sgr", "timestamp": "2017-10-17T00:23:54.234Z"}, 
 
{"id": "46537", "data": "etd", "timestamp": "2017-10-17T01:36:16.463Z"}, 
 
{"id": "83645", "data": "eth", "timestamp": "2017-10-17T01:32:25.640Z"}, 
 
{"id": "36153", "data": "her", "timestamp": "2017-10-17T01:56:13.478Z"}, 
 
{"id": "31383", "data": "sry", "timestamp": "2017-10-17T05:56:56.362Z"}, 
 
{"id": "68123", "data": "rya", "timestamp": "2017-10-17T06:34:30.652Z"}] 
 

 
var obj = a.reduce((acc, curr) => { 
 
    var t = new Date(curr.timestamp); 
 
    if(!isNaN(t.getTime())) { 
 
    var h = t.getHours(); 
 
    var index = h + ':00-' + h + ':59'; 
 
    if(!acc[index]) { 
 
     acc[index] = []; 
 
    } 
 
    acc[index].push(curr); 
 
    } 
 
    return acc; 
 
}, {}); 
 

 

 
console.log(obj);

0

Quelques corrections mineures Fireman26 de réponse:

const data = [ 
    {"id": "12345", "data": "abc", "timestamp": "2017-10-17T00:05:30.523Z"}, 
    {"id": "16375", "data": "sgr", "timestamp": "2017-10-17T00:23:54.234Z"}, 
    {"id": "46537", "data": "etd", "timestamp": "2017-10-17T01:36:16.463Z"}, 
    {"id": "83645", "data": "eth", "timestamp": "2017-10-17T01:32:25.640Z"}, 
    {"id": "36153", "data": "her", "timestamp": "2017-10-17T01:56:13.478Z"}, 
    {"id": "31383", "data": "sry", "timestamp": "2017-10-17T05:56:56.362Z"}, 
    {"id": "68123", "data": "rya", "timestamp": "2017-10-17T06:34:30.652Z"} 
] 

const grouped = data.reduce((obj, item) => { 
    const hourString = String(new Date(item.timestamp).getUTCHours()).padStart(2, '0') 
    if (hourString !== "NaN") { 
     const key = `${hourString}:00-${hourString}:59`; 
     (obj[key] = obj[key] || []).push(item) 
    } 
    return obj; 
}, {}); 
  1. L'heure est pris pour fuseau horaire UTC. L'heure est complétée avec "0", c'est-à-dire 01:00-01:59 au lieu de 1:00-1:59.