1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- Array.prototype.equals = function (array, strict = true) {
- if (!array) {
- return false
- }
- if (this.length != array.length) {
- return false
- }
- for (var i = 0; i < this.length; i++) {
- if (strict && this[i] != array[i]) {
- return false
- }
- else if (!strict) {
- return this.sort().equals(array.sort(), true)
- }
- }
- return true
- }
- Array.prototype.remove = function (obj) {
- const index = this.indexOf(obj)
- if(index != -1) {
- this.splice(index, 1)
- } else {
- console.warn(`did not find ${obj} in array ${this}`)
- }
- }
- Array.prototype.random = function() {
- return this[Math.floor(Math.random() * this.length)]
- }
- Array.prototype.shuffle = function() {
- let j, swap
- for (let i = this.length - 1; i > 0; i--) {
- j = Math.floor(Math.random() * (i + 1))
- swap = this[i]
- this[i] = this[j]
- this[j] = swap
- }
- return this
- }
- Array.prototype.groupCount = function() {
- const grouped = {}
- this.forEach(value => {
- if (!grouped[value]) {
- grouped[value] = 1
- } else {
- grouped[value]++
- }
- })
- return grouped
- }
|