JSFiddle - React, Tailwind, and code Playground
JavaScript
var arr = [
{ N: 10, Name: "Foo" },
{ N: 10, Name: "Bar" },
{ N: 20, Name: "Foo" },
{ N: 20, Name: "Bar" }
];
var poc = { name:'blah', obj:{} };
poc.obj = poc;
var arrComplex = [
{ N: { Value: 10, TooMuchRecursionProofPOC:poc }, Name: "Foo" },
{ N: { Value: 10, TooMuchRecursionProofPOC:poc }, Name: "Bar" },
{ N: { Value: 20, TooMuchRecursionProofPOC:poc }, Name: "Foo" },
{ N: { Value: 20, TooMuchRecursionProofPOC:poc }, Name: "Bar" }
];
function setup_hashers() {
// recursion protection idea
var rp = '_rp'+(Math.random()*10000000);
function tstr() {
var out = '', i = '';
if (this[rp]) return out;
this[rp] = true;
for (i in this)
if (i != rp && this.hasOwnProperty(i))
out += this[i] instanceof Object
? (!this[i][rp] ? tstr.call(this[i]) : '')
: (this[i].toString || tstr).call(this[i]);
this[rp] = undefined;
return out;
};
Number.prototype.GetHashCode = function() {
return this.valueOf();
};
Object.prototype.GetHashCode = function() {
var s = (this instanceof Object ? tstr : this.toString || tstr).call(this),
h = 0;
if (s.length)
for (var i = 0; i < s.length; i++)
h = ((h << 5) - h) + s.charCodeAt(i);
return h;
};
}
function group_by(a, keyFunc, valFunc, comp, as_array) {
if (!a.length) return as_array ? [] : {};
var keyFunc = keyFunc || function (e) { return e; },
valFunc = valFunc || function (e) { return e; };
var comp = comp || {
Equals: function (a, b) { return a == b; },
Hash: function (e) { return e.GetHashCode(); }
};
var hashs = {}, key = '', hash = '';
for (var i = 0; i < a.length; i++) {
key = keyFunc(a[i]);
hash = comp.Hash(key);
if (typeof hashs[hash] != 'undefined')
hash = comp.Equals(key, hashs[hash].Key)
? hash
: hash + '-' + i;
hashs[hash] = hashs[hash] || { Key: key, Elements: [] };
hashs[hash].Elements.push(valFunc(a[i]));
}
if...