【问题标题】:JavaScript pass new array in argument [duplicate]JavaScript在参数中传递新数组[重复]
【发布时间】:2015-10-28 14:53:31
【问题描述】:

有没有一种方法(就像我们在 C# generics/linq list.take... 中所使用的那样)可以在调用函数时在参数中获取数组的一系列元素,而无需创建新数组?

//a simple array with some elements
myArray; 
//just to show what I mean...pass the first five elemtns of array to the function
doSomethingWithArray(myArray[0,4]); 


function doSomethingWithArray(items) {
   //do stuff
}

【问题讨论】:

    标签: javascript qml qt5 qtquick2


    【解决方案1】:

    听起来您可能正在寻找slice

    doSomethingWithArray(myArray.slice(0, 4))
    

    Slice 采用startend 参数并返回数组中该范围内的项目的浅拷贝。如果你想改变数组,你可以考虑splice

    请注意,end 索引不包含,即示例中的myArray.slice(0,4),仅返回[0 .. 3] 范围内的元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多