每日一篇, 第二篇 "Array.slice"

171 阅读1分钟

1.前言

Array.prototype.slice() 数组原型上的方法

2.用法

基础用法

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

console.log(animals.slice(-3));
// 输出: ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4));
// 输出: ["camel", "duck"]

console.log(animals.slice(1, 5));
// 输出: ["bison", "camel", "duck", "elephant"]

3.参数

arr.slice([begin[, end]])

image.png

4.返回值

arr.slice([begin[, end]])

// 一个含有被提取元素的新数组

5.重写

系列文章