How do I work with Arrays?

Author: zeel
Message:

All the Array.prototype built-in loops are so useful.

Array.prototype.every()        // Returns true if all elements pass a test
Array.prototype.filter()       // Create and return a new array of elements that pass a test
Array.prototype.find()         // Return the first element to pass a test
Array.prototype.forEach()      // Apply a function to each element in order
Array.prototype.reduce()       // Accumulate a value based on all elements
Array.prototype.reduceRight()  // As above in reverse order
Array.prototype.some()         // Returns true if any element passes a test
// Not really like the others, but special mention:
Array.prototype.sort()         // Sorts the array based on a custom compare function

And combining those with Object.values() or Object.entries() makes life so much easier.

Object.entries(object).find(([key, value]) => /* test */)

Author: Atros
Message:

There are also a couple additional Array methods that Foundry adds that can be useful:

Array.fromRange(n)               // Create a sequential array of length n with integer values 0 to n-1
Array.prototype.deepFlatten()    // Recursively flatten a 2d nested array by concatenating the inner arrays
Array.prototype.equals(b)        // Do the values (and ordering) of array A identically equal the values and order of array B
Array.prototype.partition(rule)  // Split an array into 2 arrays with a partitioning rule
Array.prototype.filterJoin(sep)  // Like Array.prototype.join(), but ignores nullish elements
Array.prototype.findSplice(find, replace) // Find and remove an element from an array, replacing it with a provided replacement