Skip to main content
se eliminaron 2799 caracteres en el cuerpo
Origen Enlace

Puedes ejecutar el siguiente código que usa una implementación recursiva groupBygroupBy usando la librería UnderscoreLodash.

_.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:

_.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(JSON.stringify(groupedData, null, 4));


    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},
          ];
    }
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
{
    "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
                    }
                ]
            }
        }
    }
}
 

Puedes ejecutar el siguiente código que usa una implementación recursiva groupBy usando la librería Underscore.

_.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
                    }
                ]
            }
        }
    }
}

Puedes ejecutar el siguiente código que usa una implementación recursiva groupBy usando la librería Lodash.

_.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(JSON.stringify(groupedData, null, 4));


    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},
          ];
    }
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
 

se añadieron 3 caracteres en el cuerpo
Origen Enlace
Mariano
  • 24.5k
  • 20
  • 76
  • 105

Puedes ejecutar el siguiente código que usa una implementación recursiva [groupBy]groupBy usando la librería Underscore.

Puedes ejecutar el siguiente código que usa una implementación recursiva [groupBy] usando la librería Underscore.

Puedes ejecutar el siguiente código que usa una implementación recursiva groupBy usando la librería Underscore.

se eliminaron 142 caracteres en el cuerpo
Origen Enlace
_.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},
      ];
}

_.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},
      ];
}
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>

_.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},
      ];
}
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>

_.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},
      ];
}
Con el botón "< >" se agrega código JavaScript ejecutable en la misma respuesta
Origen Enlace
Mariano
  • 24.5k
  • 20
  • 76
  • 105
Loading
se añadieron 3040 caracteres en el cuerpo
Origen Enlace
Loading
Origen Enlace
Loading