mirror of
https://github.com/photonstorm/phaser
synced 2024-11-16 01:38:23 +00:00
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:
parent
f1bb335d35
commit
1f6554f98e
2 changed files with 38 additions and 0 deletions
37
src/utils/array/Flatten.js
Normal file
37
src/utils/array/Flatten.js
Normal 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;
|
|
@ -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'),
|
||||
|
|
Loading…
Reference in a new issue