-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'}
];

expected results

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'} ]
    ]

];

as i am trying to find a group with (custType and code), i am not able to generate the expected result. Which method i should use here? can i use reduce or groupBy. I am confused about it.

Mithun S
  • 408
  • 8
  • 20
  • Your expected result is invalid. Do you want an array of arrays or you want an array of objects with `savings`, `current` as keys – adiga Feb 03 '19 at 09:53
  • please add a valid result (arrays do not have named properties in literal notation) and the code, you tried. – Nina Scholz Feb 03 '19 at 09:53

1 Answers1

0

You could build up a nested hashtable:

 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);
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151