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

2
  • Your expected result is invalid. Do you want an array of arrays or you want an array of objects with savings, current as keys Commented Feb 3, 2019 at 9:53
  • please add a valid result (arrays do not have named properties in literal notation) and the code, you tried. Commented Feb 3, 2019 at 9:53

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.