4 votes

Comment effectuer des regroupements à l'aide d'une carte ?

J'utilise ce qui suit pour regrouper des données :

const formatted = data.map(
    area => {
        return {
            "state": area["Province_State"],
            "lat": area["lat"],
            "long": area["long"],
            "data": Object.keys(area).reduce( (prev, next) => { 
            if(next === 'date' ) {
                // do something 
                prev[next] = removeTime( area[next]) 
            } else {
            prev[next]= area[next];                         
            }
            return prev;
            } , {})
        };
    }
);

Mais cela ne correspond pas au format dont j'ai besoin.

Cela produit une série de données comme celle-ci :

{state: "Alabama", ID: "c6d6ee4b-89d3-4634-807d-340a2b35d6a4", long: -86, lat: 32, name: "Alabama", …}
ID: "c6d6ee4b-89d3-4634-807d-340a2b35d6a4"
data:
Active: "3470"
Confirmed: "3563"
Country_Region: "US"
Deaths: "93"
FIPS: "1"
Hospitalization_Rate: "12.26494527"
ISO3: "USA"
Incident_Rate: "75.98802021"
Mortality_Rate: "2.610159978"
People_Hospitalized: "437"
People_Tested: "21583"
Province_State: "Alabama"
Recovered: ""
Testing_Rate: "460.3001516"
UID: "84000001"
date: "2020-04-12"
lat: "32.3182"
long: "-86.9023"
__proto__: Object
lat: 32
long: -86
name: "Alabama"
state: "Alabama"

Mais comme chaque État dispose d'un plus grand nombre de données pour chaque date J'ai besoin de toutes les dates sous data afin de l'avoir comme :

State: "Alabama"
ID: 2
Lat: 41.1533
Long: 20.1683
Province/State: ""
data: Array(109)
[0 … 99]
0: {date: "1/22/20", Confirmed: 0, Deaths: 0, Recovered: 0}
1: {date: "1/23/20", Confirmed: 0, Deaths: 0, Recovered: 0}
2: {date: "1/24/20", Confirmed: 0, Deaths: 0, Recovered: 0}

Le code que j'utilise ne regroupe pas TOUTES les dates sous la rubrique data mais il donne une série d'objs séparés

4voto

Danziger Points 5234

Il semble que vous souhaitiez regrouper les données par État plutôt que par date. Array.prototype.reduce() mais la logique devrait être légèrement différente :

const data = [{
  'Province_State': 'CA',
  lat: 1.1,
  long: 1.1,
  date: '1/22/20 12:00',
  Confirmed: 0,
  Deaths: 0,
  Recovered: 0,
}, {
  'Province_State': 'CA',
  lat: 1.2,
  long: 1.2,
  date: '1/23/20 12:00',
  Confirmed: 1,
  Deaths: 1,
  Recovered: 1,
}, {
  'Province_State': 'LA',
  lat: 1.3,
  long: 1.3,
  date: '1/22/20 12:00',
  Confirmed: 2,
  Deaths: 2,
  Recovered: 2,
}];

const formatted = data.reduce((merged, entry) => {
  const {
    // These properties will show up only once per state:
    lat,
    long,
    Province_State: state,
    // Anything else is grouped inside the `data` field for this state's entry:
    ...rest
  } = entry;

  // You can modify the `date` field here:
  rest.date = rest.date.split(' ')[0];

  if (merged.hasOwnProperty(state)) {
    // If we already added an entry for this state, simply push the other properties to `data`:
    merged[state].data.push(rest);
  } else {
    // Otherwise, add the entry for this state:
    merged[state] = { state, lat, long, data: [rest] };
  }  

  return merged;  
}, { });

console.log(formatted);

Si vous souhaitez utiliser une boucle conventionnelle au lieu de Array.prototype.reduce() :

const data = [{
  'Province_State': 'CA',
  lat: 1.1,
  long: 1.1,
  date: '1/22/20 12:00',
  Confirmed: 0,
  Deaths: 0,
  Recovered: 0,
}, {
  'Province_State': 'CA',
  lat: 1.2,
  long: 1.2,
  date: '1/23/20 12:00',
  Confirmed: 1,
  Deaths: 1,
  Recovered: 1,
}, {
  'Province_State': 'LA',
  lat: 1.3,
  long: 1.3,
  date: '1/22/20 12:00',
  Confirmed: 2,
  Deaths: 2,
  Recovered: 2,
}];

const merged = {};

for (let i = 0; i < data.length; ++i) {
  const {
    // These properties will show up only once per state:
    lat,
    long,
    Province_State: state,
    // Anything else is grouped inside the `data` field for this state's entry:
    ...rest
  } = data[i];

  // You can modify the `date` field here:
  rest.date = rest.date.split(' ')[0];

  if (merged.hasOwnProperty(state)) {
    // If we already added an entry for this state, simply push the other properties to `data`:
    merged[state].data.push(rest);
  } else {
    // Otherwise, add the entry for this state:
    merged[state] = { state, lat, long, data: [rest] };
  }  
}

console.log(merged);

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