Question: I have an array of 5 numbers and will like to select 'ANY 3' numbers randomly  

from my array to produce the result below. I've looked and can't find a code  

that will reproduce the exact results. I was thinking a for loo' with a  

function will be the best bet but how do I go about it, any help, (JavaScript,  

Java, php) will be be appreciated please 

```
var myArray = ["1","2","3","4","5"];  
var result = myArray.slice(); 

for (i=0; i < result; i++) {
  console.log(myArray[i]);
}

Results:
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5

```

Answer: 
If you like working with ES6 `Map`, then this is for you:

```js
function groupBy(arr, prop) {
    const map = new Map(Array.from(arr, obj => [obj[prop], []]));
    arr.forEach(obj => map.get(obj[prop]).push(obj));
    return Array.from(map.values());
}

const data = [{ name: "Display", group: "Technical detals", id: 60, value: 4 }, { name: "Manufacturer", group: "Manufacturer", id: 58, value: "Apple" }, { name: "OS", group: "Technical detals", id: 37, value: "Apple iOS" }];

console.log(groupBy(data, "group"));
```

```css
.as-console-wrapper { max-height: 100% !important; top: 0; }
```

The `Map` instance is created from key/value pairs that are generated from the input array. The keys are the values of the property to group by, and the values are initialised as empty arrays.

Then those arrays are populated. Finally the values of the map (i.e. those populated arrays) are returned.
A bit different way, so we have a plain list of objects, and want to group it per property, but includes all related

```
const data = [{'group':'1', 'name':'name1'},
{'group':'2', 'name':'name2'},
{'group':'2', 'name':'name3'},
,{'group':'1', 'name':'name4'}]; 

const list = data.map( i => i.group);
const uniqueList = Array.from(new Set(list));
const groups= uniqueList.map( c => { 
            return  { group:c, names:[]};
        } ); 

data.forEach( d => { 
            groups.find( g => g.group === d.group).names.push(d.name);
});

```

so result would be like:

```
[ {'group':'1', 'names':['name1', 'name4']},
{'group':'2', 'names':['name2', 'name3']}

```

Same but with TypeScript and reduce:

```
export function groupBy2 <T>( key: string, arr: T[]): { group: string, values: T[] }[] {
     return arr.reduce((storage, item) => {
         const a = storage.find(g => g.group === item[key]);
         if (a) { a.values.push(item); }
         else { storage.push({group: item[key], values: [item]}); }
         return storage;
     }, [] as {group: string, values: T[] }[]);
 }

```