Skip to main content
2 de 6
se añadieron 3040 caracteres en el cuerpo

Puedes ejecutar el siguiente código que usa una implementación recursiva groupBy usango la libreria Underscore.

http://underscorejs.org/

_.groupByMulti = function (obj, values, context) {
    if (!values.length)
        return obj;
    var byFirst = _.groupBy(obj, values[0], context),
        rest = values.slice(1);
    for (var prop in byFirst) {
        byFirst[prop] = _.groupByMulti(byFirst[prop], rest, context);
    }
    return byFirst;
};

var groupedData = _.groupByMulti(getData(), ['country', 'state', 'city', 'name']);
console.log(groupedData);


function getData() {
    return [
      {id: 1, name: 'Pedro', city:1, state: 1, country: 1},
      {id: 2, name: 'Juan', city:12, state: 2, country: 1},
      {id: 3, name: 'Ana', city:10, state: 5, country: 1},
      {id: 4, name: 'Diego', city:7, state: 9, country: 1},
      {id: 5, name: 'Jose', city:4, state: 2, country: 1},
      {id: 6, name: 'David', city:7, state: 9, country: 1},
      {id: 7, name: 'Adriana', city:1, state: 1, country: 1},
      {id: 8, name: 'Jorge', city:15, state: 2, country: 1},
      ];
}

Al ejecutar dicha función, obtendrás el siguiente arreglo:

{
    "1": {
        "1": {
            "1": {
                "Pedro": [
                    {
                        "id": 1,
                        "name": "Pedro",
                        "city": 1,
                        "state": 1,
                        "country": 1
                    }
                ],
                "Adriana": [
                    {
                        "id": 7,
                        "name": "Adriana",
                        "city": 1,
                        "state": 1,
                        "country": 1
                    }
                ]
            }
        },
        "2": {
            "4": {
                "Jose": [
                    {
                        "id": 5,
                        "name": "Jose",
                        "city": 4,
                        "state": 2,
                        "country": 1
                    }
                ]
            },
            "12": {
                "Juan": [
                    {
                        "id": 2,
                        "name": "Juan",
                        "city": 12,
                        "state": 2,
                        "country": 1
                    }
                ]
            },
            "15": {
                "Jorge": [
                    {
                        "id": 8,
                        "name": "Jorge",
                        "city": 15,
                        "state": 2,
                        "country": 1
                    }
                ]
            }
        },
        "5": {
            "10": {
                "Ana": [
                    {
                        "id": 3,
                        "name": "Ana",
                        "city": 10,
                        "state": 5,
                        "country": 1
                    }
                ]
            }
        },
        "9": {
            "7": {
                "Diego": [
                    {
                        "id": 4,
                        "name": "Diego",
                        "city": 7,
                        "state": 9,
                        "country": 1
                    }
                ],
                "David": [
                    {
                        "id": 6,
                        "name": "David",
                        "city": 7,
                        "state": 9,
                        "country": 1
                    }
                ]
            }
        }
    }
}