How to group an array of objects by key

后端 未结 24 2967
后悔当初
后悔当初 2020-11-21 05:13

Does anyone know of a (lodash if possible too) way to group an array of objects by an object key then create a new array of objects based on the grouping? For example, I hav

相关标签:
24条回答
  • 2020-11-21 05:38

    I'd leave REAL GROUP BY for JS Arrays example exactly the same this task here

    const inputArray = [ 
        { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },
        { Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" },
        { Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" },
        { Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" },
        { Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" },
        { Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" },
        { Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" },
        { Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" }
    ];
    
    var outObject = inputArray.reduce(function(a, e) {
      // GROUP BY estimated key (estKey), well, may be a just plain key
      // a -- Accumulator result object
      // e -- sequentally checked Element, the Element that is tested just at this itaration
    
      // new grouping name may be calculated, but must be based on real value of real field
      let estKey = (e['Phase']); 
    
      (a[estKey] ? a[estKey] : (a[estKey] = null || [])).push(e);
      return a;
    }, {});
    
    console.log(outObject);

    0 讨论(0)
  • 2020-11-21 05:39

    I liked @metakunfu answer, but it doesn't provide the expected output exactly. Here's an updated that get rid of "make" in the final JSON payload.

    var cars = [
        {
            'make': 'audi',
            'model': 'r8',
            'year': '2012'
        }, {
            'make': 'audi',
            'model': 'rs5',
            'year': '2013'
        }, {
            'make': 'ford',
            'model': 'mustang',
            'year': '2012'
        }, {
            'make': 'ford',
            'model': 'fusion',
            'year': '2015'
        }, {
            'make': 'kia',
            'model': 'optima',
            'year': '2012'
        },
    ];
    
    result = cars.reduce((h, car) => Object.assign(h, { [car.make]:( h[car.make] || [] ).concat({model: car.model, year: car.year}) }), {})
    
    console.log(JSON.stringify(result));
    

    Output:

    {  
       "audi":[  
          {  
             "model":"r8",
             "year":"2012"
          },
          {  
             "model":"rs5",
             "year":"2013"
          }
       ],
       "ford":[  
          {  
             "model":"mustang",
             "year":"2012"
          },
          {  
             "model":"fusion",
             "year":"2015"
          }
       ],
       "kia":[  
          {  
             "model":"optima",
             "year":"2012"
          }
       ]
    }
    
    0 讨论(0)
  • 2020-11-21 05:40

    Just try this one it works fine for me.

    let grouped = _.groupBy(cars, 'make');

    0 讨论(0)
  • 2020-11-21 05:42

    Here is a solution inspired from Collectors.groupingBy() in Java:

    function groupingBy(list, keyMapper) {
      return list.reduce((accummalatorMap, currentValue) => {
        const key = keyMapper(currentValue);
        if(!accummalatorMap.has(key)) {
          accummalatorMap.set(key, [currentValue]);
        } else {
          accummalatorMap.set(key, accummalatorMap.get(key).push(currentValue));
        }
        return accummalatorMap;
      }, new Map());
    }

    This will give a Map object.

    // Usage
    
    const carMakers = groupingBy(cars, car => car.make);

    0 讨论(0)
  • 2020-11-21 05:44

    var cars = [{
      make: 'audi',
      model: 'r8',
      year: '2012'
    }, {
      make: 'audi',
      model: 'rs5',
      year: '2013'
    }, {
      make: 'ford',
      model: 'mustang',
      year: '2012'
    }, {
      make: 'ford',
      model: 'fusion',
      year: '2015'
    }, {
      make: 'kia',
      model: 'optima',
      year: '2012'
    }].reduce((r, car) => {
    
      const {
        model,
        year,
        make
      } = car;
    
      r[make] = [...r[make] || [], {
        model,
        year
      }];
    
      return r;
    }, {});
    
    console.log(cars);

    0 讨论(0)
  • 2020-11-21 05:44
    function groupBy(data, property) {
      return data.reduce((acc, obj) => {
        const key = obj[property];
        if (!acc[key]) {
          acc[key] = [];
        }
        acc[key].push(obj);
        return acc;
      }, {});
    }
    groupBy(people, 'age');
    
    0 讨论(0)
提交回复
热议问题