# 章序号/节序号/节/笔记序号
codecademy,array
# 概念阐释
`.slice()` 数组方法返回数组的全部或部分的浅表副本,而不修改原始数组。说明是**non-mutating**属性,不会变易。*这影响着输出结果,必须写在console里或者存储在新变量中。*
## 语法
### 返回index起始元素和结束元素
```js
array.slice(start, end); //index start, index end是**last+1**
```
- start: index
- **end: index+1**
### 返回起始位置之后的所有元素
```js
array.slice(start);
```
### 返回所有结果
```js
array.slice();
```
### 负索引
```js
array.slice(-6, -3);
```
- 得到和正索引同样结果的负索引写法:
- **start:负index-1**,
- end:负index
- 和正索引一样,如果从起始位置开始返回后面所有元素,`array.slice(-start);`
# 举例子
```js
const groceryList = ['orange juice', 'bananas', 'coffee beans', 'brown rice', 'pasta', 'coconut oil', 'plantains'];
console.log(groceryList.slice(1,4));
//print [ 'bananas', 'coffee beans', 'brown rice' ]
```
- 直接写在console里才能得出提取出的数组元素。
# 类比、比较与对比
# 问题
- 为什么slice()要写在log里才得出正确结果?或者是储存在新的变量中?
```js
const groceryList = ['orange juice', 'bananas', 'coffee beans', 'brown rice', 'pasta', 'coconut oil', 'plantains'];
```
- 用正负索引得出`brown rice`后面所有? 如果只得出一个呢?
- 用正负索引得出`'bananas', 'coffee beans', 'brown rice', 'pasta'`
- 用正索引得出`[ 'bananas', 'coffee beans', 'brown rice' ]`
- 用正索引得出`'coffee beans'`后面所有? 如果只得出一个呢?
- 用`slice()`显示所有
# 问题答案
```js
//正索引
console.log(groceryList.slice(3));
console.log(groceryList.slice(1,5));
console.log(groceryList.slice(1,4));
console.log(groceryList.slice(2,3));
//负索引
console.log(groceryList.slice(-4));
console.log(groceryList.slice(-6,-2));
```
# 备注(经验集/错误集)
## 参考资料
[`.slice()` in Docs](https://www.codecademy.com/resources/docs/javascript/arrays/slice).