AS3 合并多个数组的最快方法

发布于 2024-12-06 08:36:09 字数 457 浏览 0 评论 0原文

我正在尝试编写一个可以指定任意数量的数组的函数,并且返回值将是一个包含所有指定数组的内容的数组。

我已经做到了这一点,但这似乎是一种非常缓慢且丑陋的方法:

var ar1:Array = [1,2,3,4,5,6,7,8,9];
var ar2:Array = ['a','b','c','d','e','f','g','h'];


function merge(...multi):Array
{
    var out:String = "";

    for each(var i:Array in multi)
    {
        out += i.join(',');
    }

    return out.split(',');
}

trace(merge(ar1, ar2));

是否有一种内置且更有效/更好的方法来实现这一目标?结果不需要与输入的顺序相同 - 完全未排序就可以了。

I'm trying to write a function where I can specify any amount of array, and the return value will be an array containing the contents of all of the specified arrays.

I've done this, but it seems like a really slow and ugly way of doing it:

var ar1:Array = [1,2,3,4,5,6,7,8,9];
var ar2:Array = ['a','b','c','d','e','f','g','h'];


function merge(...multi):Array
{
    var out:String = "";

    for each(var i:Array in multi)
    {
        out += i.join(',');
    }

    return out.split(',');
}

trace(merge(ar1, ar2));

Is there an inbuilt and more efficient / nice way of achieving this? The result does not need to be in the same order as the input - completely unsorted is fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疏忽 2024-12-13 08:36:09

您可以使用连接

如果参数指定一个数组,则该数组的元素将连接起来。

var ar1:Array = [1,2,3,4,5,6,7,8,9];
var ar2:Array = ['a','b','c','d','e','f','g','h'];
var ar3:Array = ['i','j','k','l'];

var ar4 = ar1.concat(ar2, ar3); // or: ar1.concat(ar2).concat(ar3);

要从二维数组中生成单个数组,您可以使用此函数:

private function flatten(arrays:Array):Array {
    var result:Array = [];
    for(var i:int=0;i<arrays.length;i++){
        result = result.concat(arrays[i]);
    }
    return result;
}

// call
var ar4 = [ar1, ar2, ar3];
var ar5 = flatten(ar4);

您还可以使用 varargs 来合并多个数组:

private function merge(...arrays):Array {
    var result:Array = [];
    for(var i:int=0;i<arrays.length;i++){
        result = result.concat(arrays[i]);
    }
    return result;
}

// call
var ar5 = merge(ar1, ar2, ar3);

You can use concat.

If the parameters specify an array, the elements of that array are concatenated.

var ar1:Array = [1,2,3,4,5,6,7,8,9];
var ar2:Array = ['a','b','c','d','e','f','g','h'];
var ar3:Array = ['i','j','k','l'];

var ar4 = ar1.concat(ar2, ar3); // or: ar1.concat(ar2).concat(ar3);

To make a single array out of a 2 dimensional array you can use this function:

private function flatten(arrays:Array):Array {
    var result:Array = [];
    for(var i:int=0;i<arrays.length;i++){
        result = result.concat(arrays[i]);
    }
    return result;
}

// call
var ar4 = [ar1, ar2, ar3];
var ar5 = flatten(ar4);

You can also use varargs to merge multiple arrays:

private function merge(...arrays):Array {
    var result:Array = [];
    for(var i:int=0;i<arrays.length;i++){
        result = result.concat(arrays[i]);
    }
    return result;
}

// call
var ar5 = merge(ar1, ar2, ar3);
戈亓 2024-12-13 08:36:09

我不知道这个方法是否比使用循环更快,但它是合并两个数组的一种(奇特的)快速方法。 (它适用于 Javascript 和 Actionscript)

var arr1:Array = [1,2,3,4,5]
var arr2:Array = [6,7,8,9,10]

arr1.push.apply(this, arr2); // merge 
// arr1.push.call(this, arr2); // don't use this. see comment below

trace(arr1) // 1,2,3,4,5,6,7,8,9,10

I don't know if this method is faster than using loops, but it is a (fancy) quick way to merge 2 arrays. (and it works in Javascript and Actionscript)

var arr1:Array = [1,2,3,4,5]
var arr2:Array = [6,7,8,9,10]

arr1.push.apply(this, arr2); // merge 
// arr1.push.call(this, arr2); // don't use this. see comment below

trace(arr1) // 1,2,3,4,5,6,7,8,9,10
青春有你 2024-12-13 08:36:09
function merge(...multi):Array
{
  var res:Array = [];

  for each(var i:Array in multi)
  {
    res = res.concat(i);
  }

  return res;
}

没有尝试过,但这样的东西会对你有帮助。

function merge(...multi):Array
{
  var res:Array = [];

  for each(var i:Array in multi)
  {
    res = res.concat(i);
  }

  return res;
}

Didnt try it, but something like this would help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文