Array.prototype.slice()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itโs been available across browsers since โจ2015ๅนด7ๆโฉ.
slice()
ๆนๆณ่ฟๅไธไธชๆฐ็ๆฐ็ปๅฏน่ฑก๏ผ่ฟไธๅฏน่ฑกๆฏไธไธช็ฑ start
ๅ end
ๅณๅฎ็ๅๆฐ็ป็ๆต
ๆท่ด๏ผๅ
ๆฌ start
๏ผไธๅ
ๆฌ end
๏ผ๏ผๅ
ถไธญ start
ๅ end
ไปฃ่กจไบๆฐ็ปๅ
็ด ็็ดขๅผใๅๅงๆฐ็ปไธไผ่ขซๆนๅใ
ๅฐ่ฏไธไธ
const animals = ["ant", "bison", "camel", "duck", "elephant"];
console.log(animals.slice(2));
// Expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// Expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5));
// Expected output: Array ["bison", "camel", "duck", "elephant"]
console.log(animals.slice(-2));
// Expected output: Array ["duck", "elephant"]
console.log(animals.slice(2, -1));
// Expected output: Array ["camel", "duck"]
console.log(animals.slice());
// Expected output: Array ["ant", "bison", "camel", "duck", "elephant"]
่ฏญๆณ
slice()
slice(start)
slice(start, end)
ๅๆฐ
start
ๅฏ้-
ๆๅ่ตทๅงๅค็็ดขๅผ๏ผไป
0
ๅผๅง๏ผ๏ผไผ่ฝฌๆขไธบๆดๆฐใ- ๅฆๆ็ดขๅผๆฏ่ดๆฐ๏ผๅไปๆฐ็ปๆซๅฐพๅผๅง่ฎก็ฎโโๅฆๆ
start < 0
๏ผๅไฝฟ็จstart + array.length
ใ - ๅฆๆ
start < -array.length
ๆ่ ็็ฅไบstart
๏ผๅไฝฟ็จ0
ใ - ๅฆๆ
start >= array.length
๏ผๅไธๆๅไปปไฝๅ ็ด ใ
- ๅฆๆ็ดขๅผๆฏ่ดๆฐ๏ผๅไปๆฐ็ปๆซๅฐพๅผๅง่ฎก็ฎโโๅฆๆ
end
ๅฏ้-
ๆๅ็ปๆญขๅค็็ดขๅผ๏ผไป
0
ๅผๅง๏ผ๏ผไผ่ฝฌๆขไธบๆดๆฐใslice()
ไผๆๅๅฐไฝไธๅ ๆฌend
็ไฝ็ฝฎใ- ๅฆๆ็ดขๅผๆฏ่ดๆฐ๏ผๅไปๆฐ็ปๆซๅฐพๅผๅง่ฎก็ฎโโๅฆๆ
end < 0
๏ผๅไฝฟ็จend + array.length
ใ - ๅฆๆ
end < -array.length
๏ผๅไฝฟ็จ0
ใ - ๅฆๆ
end >= array.length
ๆ่ ็็ฅไบend
๏ผๅไฝฟ็จarray.length
๏ผๆๅๆๆๅ ็ด ็ดๅฐๆซๅฐพใ - ๅฆๆ
end
ๅจ่ง่ๅๅๅฐไบๆ็ญไบstart
๏ผๅไธๆๅไปปไฝๅ ็ด ใ
- ๅฆๆ็ดขๅผๆฏ่ดๆฐ๏ผๅไปๆฐ็ปๆซๅฐพๅผๅง่ฎก็ฎโโๅฆๆ
่ฟๅๅผ
ไธไธชๅซๆ่ขซๆๅๅ ็ด ็ๆฐๆฐ็ปใ
ๆ่ฟฐ
slice()
ๆนๆณๆฏไธไธชๅคๅถๆนๆณใๅฎไธไผๆนๅ this
๏ผ่ๆฏ่ฟๅไธไธชๆต
ๆท่ด๏ผๅ
ถไธญๅ
ๅซไบๅๅงๆฐ็ป็ไธ้จๅ็ธๅ็ๅ
็ด ใ
slice()
ๆนๆณไผไฟ็็ฉบๆงฝใๅฆๆ่ขซๅ็็้จๅๆฏ็จ็็๏ผๅ่ฟๅ็ๆฐ็ปไนๆฏ็จ็็ใ
slice()
ๆนๆณๆฏ้็จ็ใๅฎๅช่ฆๆฑ this
ไธๆ length
ๅฑๆงๅๆดๆฐ้ฎๅฑๆงใ
็คบไพ
่ฟๅ็ฐๆๆฐ็ป็ไธ้จๅ
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const citrus = fruits.slice(1, 3);
// fruits ๅ
ๅซ ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus ๅ
ๅซ ['Orange','Lemon']
ไฝฟ็จ slice
ๅจไธไพไธญ๏ผslice
ไป myCar
ๅๅปบไบไธไธชๆฐๆฐ็ป newCar
ใไธคไธชๆฐ็ป้ฝๅ
ๅซไบไธไธช myHonda
ๅฏน่ฑก็ๅผ็จใๅฝ myHonda
็ color
ๅฑๆงๆนๅไธบ purple
๏ผๅไธคไธชๆฐ็ปไธญ็ๅฏนๅบๅ
็ด ้ฝไผ้ไนๆนๅใ
// ไฝฟ็จ slice ๆนๆณไป myCar ๅๅปบไธไธช newCarใ
const myHonda = {
color: "red",
wheels: 4,
engine: { cylinders: 4, size: 2.2 },
};
const myCar = [myHonda, 2, "cherry condition", "purchased 1997"];
const newCar = myCar.slice(0, 2);
console.log("myCar =", myCar);
console.log("newCar =", newCar);
console.log("myCar[0].color =", myCar[0].color);
console.log("newCar[0].color =", newCar[0].color);
// ๆนๅ myHonda ๅฏน่ฑก็ colorใ
myHonda.color = "purple";
console.log("The new color of my Honda is", myHonda.color);
console.log("myCar[0].color =", myCar[0].color);
console.log("newCar[0].color =", newCar[0].color);
ไธ่ฟฐไปฃ็ ่พๅบ๏ผ
myCar = [
{ color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } },
2,
'cherry condition',
'purchased 1997'
]
newCar = [ { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } }, 2 ]
myCar[0].color = red
newCar[0].color = red
The new color of my Honda is purple
myCar[0].color = purple
newCar[0].color = purple
ๅจ็ฑปๆฐ็ปๅฏน่ฑกไธ่ฐ็จ slice()
slice()
ๆนๆณไผ่ฏปๅ this
ๅฏน่ฑก็ length
ๅฑๆง๏ผ็ถๅไป start
ๅฐ end
่ฏปๅๆดๆฐ้ฎๅฑๆง๏ผๅนถๅฐๅฎไปฌๅฎไนๅจไธไธชๆฐๅๅปบ็ๆฐ็ปไธญใ
const arrayLike = {
length: 3,
0: 2,
1: 3,
2: 4,
};
console.log(Array.prototype.slice.call(arrayLike, 1, 3));
// [ 3, 4 ]
ไฝฟ็จ slice() ๆ็ฑปๆฐ็ปๅฏน่ฑก่ฝฌๅไธบๆฐ็ป
slice()
ๆนๆณ็ปๅธธไธ bind()
ๅ call()
ไธ่ตทไฝฟ็จ๏ผ็จไบๅๅปบไธไธชๅฎ็จๆนๆณ๏ผๅฐ็ฑปๆฐ็ปๅฏน่ฑก่ฝฌๆขไธบๆฐ็ปใ
// ่ฐ็จ slice() ๆนๆณๆถ๏ผไผๅฐ this ๅฏน่ฑกไฝไธบ็ฌฌไธไธชๅๆฐไผ ๅ
ฅ
const slice = Function.prototype.call.bind(Array.prototype.slice);
function list() {
return slice(arguments);
}
const list1 = list(1, 2, 3); // [1, 2, 3]
ๅจ็จ็ๆฐ็ปไธไฝฟ็จ slice()
ๅฆๆๆบๆฐ็ปๆฏ็จ็ๆฐ็ป๏ผslice()
ๆนๆณ่ฟๅ็ๆฐ็ปไนไผๆฏ็จ็ๆฐ็ปใ
console.log([1, 2, , 4, 5].slice(1, 4)); // [2, empty, 4]
่ง่
Specification |
---|
ECMAScriptยฎ 2026 Language Specification # sec-array.prototype.slice |
ๆต่งๅจๅ ผๅฎนๆง
Loadingโฆ