Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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将对象数组分组为两个值的组合_Javascript_Arrays_Ecmascript 6 - Fatal编程技术网

Javascript将对象数组分组为两个值的组合

Javascript将对象数组分组为两个值的组合,javascript,arrays,ecmascript-6,Javascript,Arrays,Ecmascript 6,预期结果 var data=[ {custType:1,code:'savings',efDate:'2/3/19',exDate'2/3/19'}, {custType:1,code:'savings',efDate:'2/3/19',exDate:'2/3/19'}, {custType:1,code:'savings',efDate:'2/3/19',exDate:'2/3/19'}, {custType:1,code:'current',efDate:'2/3/19',exDate:'2

预期结果

var data=[
{custType:1,code:'savings',efDate:'2/3/19',exDate'2/3/19'},
{custType:1,code:'savings',efDate:'2/3/19',exDate:'2/3/19'},
{custType:1,code:'savings',efDate:'2/3/19',exDate:'2/3/19'},
{custType:1,code:'current',efDate:'2/3/19',exDate:'2/3/19'},
{custType:2,code:'current',efDate:'2/3/19',exDate:'2/3/19'},
{custType:2,code:'savings',efDate:'2/3/19',exDate:'2/3/19'},
{custType:2,code:'savings',efDate:'2/3/19',exDate:'2/3/19'}
];

当我试图找到一个具有(custType和code)的组时,我无法生成预期的结果。我应该在这里使用哪种方法?我可以使用reduce或groupBy吗。我对此感到困惑。

您可以构建一个嵌套哈希表:

var modifiedData=[
    [
    savings=[ {custType:1,code:'savings',efDate:'2/3/19',exDate:'2/3/19'},
              {custType:1,code:'savings',efDate:'2/3/19',exDate:'2/3/19'},
              {custType:1,code:'savings',efDate:'2/3/19',exDate:'2/3/19'}
 ],
    current=[ {custType:1,code:'current',efDate:'2/3/19',exDate:'2/3/19'} ]
    ],
  [
    savings=[ {custType:2,code:'savings',efDate:'2/3/19',exDate:'2/3/19'},
              {custType:2,code:'savings',efDate:'2/3/19',exDate:'2/3/19'} ],
    current=[ {custType:2,code:'current',efDate:'2/3/19',exDate:'2/3/19'} ]
    ]

];

您的预期结果无效。您想要一个数组数组,还是想要一个带有
储蓄
当前
键的对象数组请添加一个有效的结果(数组没有以文字表示法命名的属性)和代码,您已尝试过。
 const hash = {};

 for(const value of data) {
  const group = hash[value.custType] || (hash[value.custType] = {});
  const group2 = group[value.code] || (group[value.code] = []);
  group2.push(value);
}

const result = Object.values(hash).map(Object.values);