Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中有没有一种方法可以按多字段按lodash对数据进行分组_Javascript_Angular_Typescript_Lodash - Fatal编程技术网

在Javascript中有没有一种方法可以按多字段按lodash对数据进行分组

在Javascript中有没有一种方法可以按多字段按lodash对数据进行分组,javascript,angular,typescript,lodash,Javascript,Angular,Typescript,Lodash,我想按多个字段对数据进行分组。因此,我添加了数据和预期结果 const skus = [ { id: 1, customAttributes: { Color: 'Red', Size: 'S', Season: 2019 } }, { id: 2, customAttributes: { Color: 'Red', Size: 'S', Season: 2020

我想按多个字段对数据进行分组。因此,我添加了数据和预期结果

const skus = [
  {
    id: 1,
    customAttributes: {
      Color: 'Red',
      Size: 'S',
      Season: 2019
    }
  },
  {
    id: 2,
    customAttributes: {
      Color: 'Red',
      Size: 'S',
      Season: 2020
    }
  },
  {
    id: 3,
    customAttributes: {
      Color: 'Red',
      Size: 'M',
      Season: 2019
    }
  },
  {
    id: 4,
    customAttributes: {
      Color: 'Red',
      Size: 'M',
      Season: 2020
    }
  },
  {
    id: 5,
    customAttributes: {
      Color: 'Red',
      Size: 'L',
      Season: 2019
    }
  },
  {
    id: 6,
    customAttributes: {
      Color: 'Red',
      Size: 'L',
      Season: 2020
    }
  },
  {
    id: 7,
    customAttributes: {
      Color: 'Green',
      Size: 'S',
      Season: 2019
    }
  },
  {
    id: 8,
    customAttributes: {
      Color: 'Green',
      Size: 'S',
      Season: 2020
    }
  },
  {
    id: 9,
    customAttributes: {
      Color: 'Green',
      Size: 'M',
      Season: 2019
    }
  },
  {
    id: 10,
    customAttributes: {
      Color: 'Green',
      Size: 'M',
      Season: 2020
    }
  },
  {
    id: 11,
    customAttributes: {
      Color: 'Green',
      Size: 'L',
      Season: 2019
    }
  },
  {
    id: 12,
    customAttributes: {
      Color: 'Green',
      Size: 'L',
      Season: 2020
    }
  }
];

console.log( // groupBy is working by 1 parameter, but I don't know to make it multiple parameter
  _.chain(skus)
    .map(sku => sku.customAttributes)
    .groupBy('Color')
    .map((value, key) => ({ title: key, skus: value }))
    .value()
);

// Just like: groupBy('Color', 'Size')
我想通过多个参数对这些SKU进行分组,就像使用lodash对颜色和大小进行分组一样

通过1个参数,它运行良好。但是当有多个参数的时候,我就不能做对

预期结果是:

谢谢。

删除到customAttributes的映射,因为您希望结果中包含id为的对象。将函数传递给u.groupBy,并返回包含要用作组键的字段的字符串

const skus=[{id:1,customAttributes:{Color:Red,Size:S,Season:2019}},{id:2,customAttributes:{Color:Red,Size:S,Season:S,Season:2020},{id:3,customAttributes:{Color:Red,Size:M,Season:2019},{id:4,customAttributes:{Color:Red,Size:M,Season:2020},{id:5,customAttributes:{Color:Red,Size:L,Season:2019,{id:6,customAttributes,Season:L},{id:7,customAttributes:{Color:Green,Size:S,Season:2019},{id:8,customAttributes:{Color:Green,Size:S,Season:2020},{id:9,customAttributes:{Color:Green,Size:M,Season:2019},{id:10,customAttributes:{Color:Green,Size:M,Season:2020},{id:11,customAttributes:{Color:Green,Size:L,Season:2019},{id:12,customAttributes:Green,Season:2020}; 常量结果=_SKU .groupBy{customAttributes:a}=>`${a.Color}/${a.Size}` .mapskus,title=>{title,skus} 价值 console.logresult;
很明显,我要花这么多时间来买这瓶酒。非常感谢。它很有效。
[
    {
        title: 'Red / S', // Color / Size
        skus: [
            {
                id: 1,
                customAttributes: {
                  Color: 'Red',
                  Size: 'S',
                  Season: 2019
                }
            },
            {
                id: 2,
                customAttributes: {
                  Color: 'Red',
                  Size: 'S',
                  Season: 2020
                }
            }
        ]
    },
    {
        title: 'Red / M', // Color / Size
        skus: [
            {
                id: 3,
                customAttributes: {
                  Color: 'Red',
                  Size: 'M',
                  Season: 2019
                }
            },
            {
                id: 4,
                customAttributes: {
                  Color: 'Red',
                  Size: 'M',
                  Season: 2020
                }
            }
        ]
    },
    ....
]