-2

I have an array sorted as shown below. each element in the array is an object contains several properties. one of these properties is called "priorityTag". according to the code belwo, i am sorting the the array according to the priorityTag in each object. The code works properly.

some of the objects contains the same priorityTag. what i am trying to do is, to group the similar objects that contains the same priorityTag together.

for example, if the array contains the following:

example

arr[{prop1: "aaa1", prop2:"yas1", priorityTag:100},//group1
{prop1: "aaa2", prop2:"yas2", priorityTag:200},//group2
{prop1: "aaa3", prop2:"yas3", priorityTag:100},//group1
{prop1: "aaa4", prop2:"yas4", priorityTag:200},//group2
{prop1: "aaa5", prop2:"yas5", priorityTag:300},//group3
{prop1: "aaa6", prop2:"yas6", priorityTag:400},//group4
{prop1: "aaa7", prop2:"yas7", priorityTag:300},//group3
{prop1: "aaa8", prop2:"yas8", priorityTag:400},//group4
{prop1: "aaa9", prop2:"yas9", priorityTag:500},//group5
{prop1: "aaa10", prop2:"yas10", priorityTag:700},//group6
{prop1: "aaa11", prop2:"yas11", priorityTag:700},//group6
]

I want to get an array contains of groups of the objects that have the same priorityTag.

I want to have Array of Arrays.each element in the main Array is Array(s) contains the the object(s) that has the same priorityTag

Please let me know how to do that correctly

code

sortFeaturesPerPriority2(arr) {
    logger.debug('[sortFeaturesPerPriority2]: Array.isArray(arr):', Array.isArray(arr));
    logger.debug('[sortFeaturesPerPriority2]: arr:', arr);
    logger.debug('[sortFeaturesPerPriority2]: arr.length:', arr.length);

    arr.sort((a, b) => a.getPriorityTag() - b.getPriorityTag());
    logger.debug('[sortFeaturesPerPriority2]: arr.sort:', arr);

    return arr;
  }
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • What do you want your output to be? An array of arrays? An object with the priorityTag as the keys and an array of the matching elements as the values? – mhodges May 17 '18 at 17:16
  • @mhodges i want to have Array of Arrays.each element in the main Array is Array(s) cntains the the object(s) that has the same priorityTag – Amrmsmb May 17 '18 at 17:17
  • I tried to fit your code into a [mcve] - using the `<>` snippet editor. Your array does not seem valid and you should change the logger to console.log to show it here – mplungjan May 17 '18 at 17:19
  • @LetsamrIt Okay, check out my solution and see if that works for you – mhodges May 17 '18 at 17:21
  • Possible duplicate of [How to group an array of objects by key](https://stackoverflow.com/questions/40774697/how-to-group-an-array-of-objects-by-key) – ASDFGerte May 17 '18 at 17:22

2 Answers2

1

Here is one way to do it. You can group the items into an object by key, and then if you need an array of arrays, you can simply map the keys into an array of arrays, like so:

arr = [{prop1: "aaa1", prop2:"yas1", priorityTag:100},//group1
{prop1: "aaa2", prop2:"yas2", priorityTag:200},//group2
{prop1: "aaa3", prop2:"yas3", priorityTag:100},//group1
{prop1: "aaa4", prop2:"yas4", priorityTag:200},//group2
{prop1: "aaa5", prop2:"yas5", priorityTag:300},//group3
{prop1: "aaa6", prop2:"yas6", priorityTag:400},//group4
{prop1: "aaa7", prop2:"yas7", priorityTag:300},//group3
{prop1: "aaa8", prop2:"yas8", priorityTag:400},//group4
{prop1: "aaa9", prop2:"yas9", priorityTag:500},//group5
{prop1: "aaa10", prop2:"yas10", priorityTag:700},//group6
{prop1: "aaa11", prop2:"yas11", priorityTag:700},//group6
];

// arrays grouped in object by key
var grouped = arr.reduce((result, curr) => {
  // if key doesn't exist yet on obj, create it and initialize to empty array
  result[curr.priorityTag] = result[curr.priorityTag] || [];
  // push the current element to the proper group
  result[curr.priorityTag].push(curr);
  return result;
}, {});
//console.log(grouped);

// map object key/values into array of arrays
var groupedArrs = Object.values(grouped);

console.log(groupedArrs);
mhodges
  • 10,938
  • 2
  • 28
  • 46
  • you could use `Object.values` instead of `Object.keys(grouped).map(key => grouped[key]);` – Nina Scholz May 17 '18 at 17:21
  • @NinaScholz True - force of habit. I'll update it, thanks. – mhodges May 17 '18 at 17:22
  • actually i get one main Array contain one element, and that element contains 11 elements...it is not grouped..i expected to get one main Array and it contains 6 elements. each elements is an arry of objects that has the same priorityTag – Amrmsmb May 18 '18 at 08:21
  • @LetsamrIt That's exactly what this does, no? Can you check my output by pressing "Run code snippet" and see if that's the output format you're expecting? – mhodges May 18 '18 at 15:20
0

Heres a pretty bare-bones solution

result = []
arr.forEach( (element) => { 
    if(result[element.priorityTag] == undefined)
    { 
        result[element.priorityTag] = []; 
    } 
    result[element.priorityTag].push( element ) 
});

The result will be an array. If you prefer an object, simply replace result = [] with result = {}