ๆญค้กต้ข็”ฑ็คพๅŒบไปŽ่‹ฑๆ–‡็ฟป่ฏ‘่€Œๆฅใ€‚ไบ†่งฃๆ›ดๅคšๅนถๅŠ ๅ…ฅ MDN Web Docs ็คพๅŒบใ€‚

View in English Always switch to English

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"]

่ฏญๆณ•

js
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 ๅฑžๆ€งๅ’Œๆ•ดๆ•ฐ้”ฎๅฑžๆ€งใ€‚

็คบไพ‹

่ฟ”ๅ›ž็Žฐๆœ‰ๆ•ฐ็ป„็š„ไธ€้ƒจๅˆ†

js
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๏ผŒๅˆ™ไธคไธชๆ•ฐ็ป„ไธญ็š„ๅฏนๅบ”ๅ…ƒ็ด ้ƒฝไผš้šไน‹ๆ”นๅ˜ใ€‚

js
// ไฝฟ็”จ 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);

ไธŠ่ฟฐไปฃ็ ่พ“ๅ‡บ๏ผš

js
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 ่ฏปๅ–ๆ•ดๆ•ฐ้”ฎๅฑžๆ€ง๏ผŒๅนถๅฐ†ๅฎƒไปฌๅฎšไน‰ๅœจไธ€ไธชๆ–ฐๅˆ›ๅปบ็š„ๆ•ฐ็ป„ไธญใ€‚

js
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() ไธ€่ตทไฝฟ็”จ๏ผŒ็”จไบŽๅˆ›ๅปบไธ€ไธชๅฎž็”จๆ–นๆณ•๏ผŒๅฐ†็ฑปๆ•ฐ็ป„ๅฏน่ฑก่ฝฌๆขไธบๆ•ฐ็ป„ใ€‚

js
// ่ฐƒ็”จ 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() ๆ–นๆณ•่ฟ”ๅ›ž็š„ๆ•ฐ็ป„ไนŸไผšๆ˜ฏ็จ€็–ๆ•ฐ็ป„ใ€‚

js
console.log([1, 2, , 4, 5].slice(1, 4)); // [2, empty, 4]

่ง„่Œƒ

Specification
ECMAScriptยฎ 2026 Language Specification
# sec-array.prototype.slice

ๆต่งˆๅ™จๅ…ผๅฎนๆ€ง

ๅ‚่ง