如何从数组中获取子数组?

Ser*_*lov 238 javascript arrays

我有var ar = [1, 2, 3, 4, 5]并且想要一些函数getSubarray(array, fromIndex, toIndex),调用的结果getSubarray(ar, 1, 3)是新数组[2, 3, 4].

Ale*_* K. 372

看一眼 Array.slice(begin, end)

const ar  = [1, 2, 3, 4, 5];

// slice from 1..3 - add 1 as the end index is not included

const ar2 = ar.slice(1, 3 + 1);

console.log(ar2);
Run Code Online (Sandbox Code Playgroud)

  • 这里可能值得明确提到原始的`ar`是未经修改的.`console.log(ar);``// - > [1,2,3,4,5]` (7认同)
  • 另外,不要犯将 `slice()` 方法与 `splice()` 混淆的错误:`splice()` 会更改原始数组,而 `slice()` 不会。 (2认同)
  • @Igor 不,切片不会引发异常。如果“end”大于序列的长度,则切片会提取到序列的末尾(“arr.length”)。https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice (2认同)

Abd*_*UMI 16

对于slice的简单使用,请使用我对Array Class的扩展:

Array.prototype.subarray=function(start,end){
     if(!end){ end=-1;} 
    return this.slice(start, this.length+1-(end*-1));
}
Run Code Online (Sandbox Code Playgroud)

然后 :

var bigArr=["a", "b", "c", "fd", "ze"]
Run Code Online (Sandbox Code Playgroud)

测试1:

bigArr.subarray(1,-1)
Run Code Online (Sandbox Code Playgroud)

<["b","c","fd","ze"]

测试2:

bigArr.subarray(2,-2)
Run Code Online (Sandbox Code Playgroud)

<["c","fd"]

TEST3:

bigArr.subarray(2)
Run Code Online (Sandbox Code Playgroud)

<["c","fd","ze"]

来自其他语言的开发人员可能更容易(.ie:Groovy)

  • [不要修改你不拥有的对象](https://www.nczonline.net/blog/2010/03/02/maintainable-javascript-dont-modify-objects-you-down-own/) (7认同)
  • K_7 说了什么;最特别的是,对内置函数(对象、数组、Promise 等)进行猴子修补是“非常”顽皮的。请参阅 [MooTools 强制重命名](https://github.com/tc39/Array.prototype.includes/commit/4b6b9534582cb7991daea3980c26a34af0e76c6c) 的著名示例,将建议的原生 `Array.prototype.contains` 改为 `Array.prototype.includes `。 (2认同)
  • 更不用说,您的“子数组”方法会带来意想不到的结果。`bigArr.slice(1,-1)` 返回 `['b','c','fd']`,正如您所期望的那样(-1 将新数组末尾的一个元素删除)。但是 `bigArr.subarray(1,-1)` 返回与 `bigArr.subarray(1)` 相同的结果,也就是说从位置 1 到 `bigArr` 末尾的*所有内容。您还强制用户始终提供负数作为“end”参数。任何 `end &gt;= -1` 都会给出与 `end === undefined` 相同的结果。另一方面,“bigArr.slice(1,3)”返回“['b','c']”,这也是预期的。 (2认同)

Sta*_*kin 7

我有 var ar = [1, 2, 3, 4, 5] 并想要一些函数 getSubarray(array, fromIndex, toIndex),调用 getSubarray(ar, 1, 3) 的结果是新数组 [2, 3, 4 ]。

精确解

function getSubarray(array, fromIndex, toIndex) {
    return array.slice(fromIndex, toIndex+1);
}
Run Code Online (Sandbox Code Playgroud)

让我们测试一下解决方案

let ar = [1, 2, 3, 4, 5]
getSubarray(ar, 1, 3)

// result: [2,3,4]
Run Code Online (Sandbox Code Playgroud)

Array.prototype.slice()

slice() 方法将数组的一部分的浅表副本返回到从开始到结束(不包括结束)选择的新数组对象中,其中开始和结束表示该数组中项目的索引。原始数组不会被修改。

基本上,切片允许您从数组中选择一个子数组。

例如,我们以这个数组为例:

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

这样做:

console.log(animals.slice(2, 4));

会给我们这个输出:

// result: ["camel", "duck"]

句法:

slice() // creates a shallow copy of the array
slice(start) // shows only starting point and returns all values after start index
slice(start, end) // slices from start index to end index
Run Code Online (Sandbox Code Playgroud)

请参阅浅复制参考


han*_*man 5

const array_one = [11, 22, 33, 44, 55];
const start = 1;
const end = array_one.length - 1;
const array_2 = array_one.slice(start, end);
console.log(array_2);
Run Code Online (Sandbox Code Playgroud)