Utils.Array.Flatten is a new function that will return a flattened version of an array, regardless of how deeply-nested it is.

This commit is contained in:
Richard Davey 2022-07-26 18:34:04 +01:00
parent f1bb335d35
commit 1f6554f98e
2 changed files with 38 additions and 0 deletions

View file

@ -0,0 +1,37 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2022 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
/**
* Takes an array and flattens it, returning a shallow-copy flattened array.
*
* @function Phaser.Utils.Array.Flatten
* @since 3.60.0
*
* @param {array} array - The array to flatten.
* @param {array} [output] - An array to hold the results in.
*
* @return {array} The flattened output array.
*/
var Flatten = function (array, output)
{
if (output === undefined) { output = []; }
for (var i = 0; i < array.length; i++)
{
if (Array.isArray(array[i]))
{
Flatten(array[i], output);
}
else
{
output.push(array[i]);
}
}
return output;
};
module.exports = Flatten;

View file

@ -19,6 +19,7 @@ module.exports = {
Each: require('./Each'),
EachInRange: require('./EachInRange'),
FindClosestInSorted: require('./FindClosestInSorted'),
Flatten: require('./Flatten'),
GetAll: require('./GetAll'),
GetFirst: require('./GetFirst'),
GetRandom: require('./GetRandom'),