web:javascript:array_prototype_ext
function addPrototypeFunctions() { if (typeof Object.defineProperty === 'function') { try { Object.defineProperty(Array.prototype, 'sortBy', { value:sb }); Object.defineProperty(Array.prototype, 'sameAs', { value:sameAs }); } catch (e) { } } if (!Array.prototype.sortBy) { Array.prototype.sortBy = sb; } if (!Array.prototype.sameAs) { Array.prototype.sameAs = sameAs; } } /* http://stackoverflow.com/questions/10123953/sort-javascript-object-array-by-date */ function sb(f) { var that = this; var i; for (i = that.length; i;) { var o = that[--i]; that[i] = [].concat(f.call(o, o, i), o); } that.sort(function (a, b) { for (var i = 0, len = a.length; i < len; ++i) { if (a[i] !== b[i]) { return a[i] < b[i] ? -1 : 1; } } return 0; }); for (i = that.length; i;) { that[--i] = that[i][that[i].length - 1]; } return that; } /* http://stackoverflow.com/questions/6229197/how-to-know-if-two-arrays-have-the-same-values */ function sameAs (testArr) { var that = this; if (that.length !== testArr.length) { return false; } for (var i = 0; i < testArr.length; i++) { if (that[i].compare) { if (!that[i].compare(testArr[i])) { return false; } } if (that[i] !== testArr[i]) { return false; } } return true; }
web/javascript/array_prototype_ext.txt · Dernière modification : 2022/02/02 00:42 de 127.0.0.1