Actually, LINQ-Group-By returns something like a dictionary with the group-item as key, and the element-array as object in a list.
This can be done easily in JavaScript.
Here's the TypeScript-Source:
// https://stackoverflow.com/questions/20310369/declare-a-delegate-type-in-typescript
// type Predicate<T, TKey> = (item: T) => TKey;
interface Predicate<T, TKey>
{
(item: T): TKey;
}
function LinqGroupBy<TSource, TKey>(source: TSource[], keySelector: Predicate<TSource, TKey>)
: { [key: string]: TSource[] }
{
if (source == null)
throw new Error("ArgumentNullException: Source");
if (keySelector == null)
throw new Error("ArgumentNullException: keySelector");
let dict: { [key: string]: TSource[]} = {};
for (let i = 0; i < source.length; ++i)
{
let key: string = String(keySelector(source[i]));
if (!dict.hasOwnProperty(key))
{
dict[key] = [];
}
dict[key].push(source[i]);
}
return dict;
}
Which transpiles down to:
function LinqGroupBy(source, keySelector)
{
if (source == null)
throw new Error("ArgumentNullException: Source");
if (keySelector == null)
throw new Error("ArgumentNullException: keySelector");
var dict = {};
for (var i = 0; i < source.length; ++i)
{
var key = String(keySelector(source[i]));
if (!dict.hasOwnProperty(key))
{
dict[key] = [];
}
dict[key].push(source[i]);
}
return dict;
}
which is a bit shaky due to the nature of objects in JavaScript, but it works in most cases.