2015-12-01 33 views
6

considerare questo oggetto JSON di seguito:Mappa e ridurre JSON Oggetti con JavaScript

{ 
    "cells":[ 
     { 
     "count":"1", 
     "gdp_growth__avg":1.90575802503285, 
     "geo__name":"united states of america", 
     "time":1990 
     }, 
     { 
     "count":"1", 
     "gdp_growth__avg":9.17893670154459, 
     "geo__name":"china", 
     "time":1991 
     }, 
     { 
     "count":"1", 
     "gdp_growth__avg":-5.04693945214571, 
     "geo__name":"russia", 
     "time":1991 
     }, 
     { 
     "count":"1", 
     "gdp_growth__avg":-0.0622142217811472, 
     "geo__name":"botswana", 
     "time":1991 
     }, 
     { 
     "count":"1", 
     "gdp_growth__avg":14.2407063986337, 
     "geo__name":"china", 
     "time":1992 
     }, 
     { 
     "count":"1", 
     "gdp_growth__avg":-14.5310737731921, 
     "geo__name":"russia", 
     "time":1992 
     }, 
     { 
     "count":"1", 
     "gdp_growth__avg":3.55494453739944, 
     "geo__name":"united states of america", 
     "time":1992 
     }, 
     { 
     "count":"1", 
     "gdp_growth__avg":13.9643147001603, 
     "geo__name":"china", 
     "time":1993 
     }, 
     { 
     "count":"1", 
     "gdp_growth__avg":-8.66854034194856, 
     "geo__name":"botswana", 
     "time":1993 
     }, 
     { 
     "count":"1", 
     "gdp_growth__avg":2.74204850437989, 
     "geo__name":"united states of america", 
     "time":1993 
     }, 
     { 
     "count":"1", 
     "gdp_growth__avg":4.04272516401846, 
     "geo__name":"united states of america", 
     "time":1994 
     }, 
     { 
     "count":"1", 
     "gdp_growth__avg":13.0806818010789, 
     "geo__name":"china", 
     "time":1994 
     }, 
     { 
     "count":"1", 
     "gdp_growth__avg":-12.5697559787493, 
     "geo__name":"russia", 
     "time":1994 
     }, 
     { 
     "count":"1", 
     "gdp_growth__avg":10.9249803004994, 
     "geo__name":"china", 
     "time":1995 
     }, 
     { 
     "count":"1", 
     "gdp_growth__avg":-4.14352840666389, 
     "geo__name":"russia", 
     "time":1995 
     }, 
     { 
     "count":"1", 
     "gdp_growth__avg":2.71655384149574, 
     "geo__name":"united states of america", 
     "time":1995 
     }, 
     { 
     "count":"1", 
     "gdp_growth__avg":10.0085233990531, 
     "geo__name":"china", 
     "time":1996 
     }, 
     { 
     "count":"1", 
     "gdp_growth__avg":3.79848988541973, 
     "geo__name":"united states of america", 
     "time":1996 
     } 
] 
} 

lo farei per mappare e Ridurre e generare un nuovo oggetto che contiene un riepilogo di crescita del PIL per tutti i paesi del JSON sopra di tale potrebbe apparire più o meno così:

{ 
    { 
    "gdp_growth__avg":46.23, 
    "geo__name":"united states of america", 
    }, 
    { 
    "gdp_growth__avg":16.23, 
    "geo__name":"china", 
    }, 
    { 
    "gdp_growth__avg":36.23, 
    "geo__name":"russia", 
    }, 
    { 
    "gdp_growth__avg":26.23, 
    "geo__name":"botswana", 
    "time":1991 
    } 
} 

ho guardato map e reduce e non sono sicuro di come meglio procedere.

Stavo pensando qualcosa di simile potrebbe essere in movimento nella giusta direzione, ma non sembra fare quello che mi aspettavo:

 var arr = [{x:1},{x:2},{x:4}]; 

     arr.reduce(function (a, b) { 
     return {x: a.x + b.x}; 
     }); 

     console.log(arr); //Outputs that same initial array 

Pur riconoscendo che è probabilmente migliore e più facile da fare questo su dal lato server, mi chiedo se quello che sto cercando di fare possa essere fatto sul lato client con JavaScript. Eventuali suggerimenti? Grazie in anticipo.

+4

Volete 'console.log (arr.reduce (...))'. 'reduce' non altera la matrice (o addirittura riassegna la variabile), semplicemente * restituisce * un valore. – Bergi

+0

@Berg, +1 perché ho imparato qualcosa che non mi aspettavo. Grazie. – MrEhawk82

+0

'map' e' reduce' possono essere utilizzati anche lato server se si utilizza il nodo. Probabilmente ciò che il tuo server può eseguire può farlo. Più conveniente in js o python (ci sono server basati su python?) – sqykly

risposta

3

Prova questa: esempio

var data = { cells:[...] }; 

var r = data.cells.reduce(function(pv, cv) { 
    if (pv[cv.geo__name]) { 
     pv[cv.geo__name] += cv.gdp_growth__avg; 
    } else { 
     pv[cv.geo__name] = cv.gdp_growth__avg; 
    } 
    return pv; 
}, {}); 

console.log(r); 

uscita:

{ 
     'united states of america': 18.76051995774611, 
     'china': 71.39814330096999, 
     'russia': -36.291297610751, 
     'botswana': -8.730754563729707 
    } 
3

Il metodo Array.reduce non modifica l'oggetto array, restituisce i risultati in un nuovo array.

+1

No, non restituisce un nuovo array. – Bergi

0

si può provare qualcosa di simile:

var data = {"cells": [...]}; 

data.cells.map(function(datum) { 
    return { 
    geo__name: datum.geo__name, 
    gdp_growth__avg: data.cells.filter(function(o) { 
     return o.geo__name === datum.geo__name; 
    }).reduce(function(sum, o) { 
     return sum + o.gdp_growth__avg; 
    }, 0) 
    }; 
}) 

Inutile dire, è possibile estrarre altri oggetti di datum pure, come ad esempio time. Non ho.

Problemi correlati