Jayanth Vishwanath
Jayanth Vishwanath

Reputation: 63

create dynamic array in typescript?

I have an array below.

let arr = [{_firstName: "john", _lastName: "tom", _selectedDate: "03/12/2018", _slot: "6AM to 8AM", _phoneNumber: "+19173432970", _sport: "Soccer"},
           {_firstName: "Jason", _lastName: "Deli", _selectedDate: "03/12/2018", _slot: "6AM to 8AM", _phoneNumber: "+19134592970", _sport: "Soccer"},
           {_firstName: "Shey", _lastName: "Ford", _selectedDate: "03/12/2018", _slot: "6AM to 8AM", _phoneNumber: "+191767542970", _sport: "Soccer"},
           {_firstName: "Jake", _lastName: "Hoss", _selectedDate: "03/13/2018", _slot: "6AM to 8AM", _phoneNumber: "+191723422970", _sport: "Soccer"},
           {_firstName: "Vamsee", _lastName: "Karru", _selectedDate: "03/13/2018", _slot: "6AM to 8AM", _phoneNumber: "+18767692970", _sport: "Soccer"}];

I would like to compare all objects in array and find similar obj, put it in another array.

Example:

Let's have a look at first 3 objects in the above array. They all 3 are similar because they have same selectedDate, slot and sport. If all those 3 are similar while comparision, I would like to create a dynamic array. At last I can get all the similar array objects in different array.

The last two objects of the array are different from first 3 objects as they have different dates (selectedDate). Below is how I need the dynamic arrays.

arr1 = [{_firstName: "john", _lastName: "tom", _selectedDate: "03/12/2018", _slot: "6AM to 8AM", _phoneNumber: "+19173432970", _sport: "Soccer"},
        {_firstName: "Jason", _lastName: "Deli", _selectedDate: "03/12/2018", _slot: "6AM to 8AM", _phoneNumber: "+19145692970", _sport: "Soccer"},
        {_firstName: "Shey", _lastName: "Ford", _selectedDate: "03/12/2018", _slot: "6AM to 8AM", _phoneNumber: "+19177612370", _sport: "Soccer"}];

arr2 = [{_firstName: "Jake", _lastName: "Hoss", _selectedDate: "03/13/2018", _slot: "6AM to 8AM", _phoneNumber: "+191776639270", _sport: "Soccer"},
        {_firstName: "Vamsee", _lastName: "Karru", _selectedDate: "03/13/2018", _slot: "6AM to 8AM", _phoneNumber: "+19100692465", _sport: "Soccer"}];

Is there any way I can accomplish this by creating dynamic arrays?

Please help. Thank You.

Upvotes: 1

Views: 1212

Answers (1)

Ele
Ele

Reputation: 33726

You can use the function reduce and group by those keys.

let arr = [{_firstName: "john", _lastName: "tom", _selectedDate: "03/12/2018", _slot: "6AM to 8AM", _phoneNumber: "+19173432970", _sport: "Soccer"},           {_firstName: "Jason", _lastName: "Deli", _selectedDate: "03/12/2018", _slot: "6AM to 8AM", _phoneNumber: "+19177692970", _sport: "Soccer"},           {_firstName: "Shey", _lastName: "Ford", _selectedDate: "03/12/2018", _slot: "6AM to 8AM", _phoneNumber: "+19177692970", _sport: "Soccer"},           {_firstName: "Jake", _lastName: "Hoss", _selectedDate: "03/13/2018", _slot: "6AM to 8AM", _phoneNumber: "+19177692970", _sport: "Soccer"},           {_firstName: "Vamsee", _lastName: "Karru", _selectedDate: "03/13/2018", _slot: "6AM to 8AM", _phoneNumber: "+19177692970", _sport: "Soccer"}];
           
let result = arr.reduce((a, c) => {
  let groupKey = ['_selectedDate', '_slot', '_sport'].map(g => c[g]).join('|');
  (a[groupKey] || (a[groupKey] = [])).push(c);  
  return a;
}, {});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 3

Related Questions