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() ë©ìëë ì´ë¤ ë°°ì´ì begin ë¶í° end ê¹ì§(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"]
구문
arr.slice([begin[, end]])
매ê°ë³ì
beginOptional-
0ì ììì¼ë¡ íë ì¶ì¶ ììì ì ëí ì¸ë±ì¤ë¥¼ ì미í©ëë¤. ìì ì¸ë±ì¤ë ë°°ì´ì ëììë¶í°ì 길ì´ë¥¼ ëíë ëë¤.
slice(-2)ë ë°°ì´ìì ë§ì§ë§ ë ê°ì ì리먼í¸ë¥¼ ì¶ì¶í©ëë¤.beginì´undefinedì¸ ê²½ì°ìë, 0ë² ì¸ë±ì¤ë¶í°sliceí©ëë¤.beginì´ ë°°ì´ì 길ì´ë³´ë¤ í° ê²½ì°ìë, ë¹ ë°°ì´ì ë°íí©ëë¤. endOptional-
ì¶ì¶ì ì¢ ë£ í 0 ê¸°ì¤ ì¸ë±ì¤ì ëë¤.
sliceëendì¸ë±ì¤ë¥¼ ì ì¸íê³ ì¶ì¶í©ëë¤. ì를 ë¤ì´,slice(1,4)ë ëë²ì§¸ ììë¶í° ë¤ë²ì§¸ ììê¹ì§ (1, 2 ë° 3ì ì¸ë±ì¤ë¡ íë ìì) ì¶ì¶í©ëë¤. ìì ì¸ë±ì¤ë ë°°ì´ì ëììë¶í°ì 길ì´ë¥¼ ëíë ëë¤. ì를ë¤ì´slice(2,-1)ë ì¸ë²ì§¸ë¶í° ëìì ëë²ì§¸ ììê¹ì§ ì¶ì¶í©ëë¤.endê° ìëµëë©´slice()ë ë°°ì´ì ëê¹ì§(arr.length) ì¶ì¶í©ëë¤.
ë§ì½ end ê°ì´ ë°°ì´ì 길ì´ë³´ë¤ í¬ë¤ë©´, slice()ë ë°°ì´ì ëê¹ì§(arr.length) ì¶ì¶í©ëë¤.
ë°í ê°
ì¶ì¶í ìì를 í¬í¨í ìë¡ì´ ë°°ì´.
ì¤ëª
slice()ë ì본ì ëì²´íì§ ììµëë¤. ì본 ë°°ì´ìì ììì ìì ë³µì¬ë³¸ì ë°íí©ëë¤. ì본 ë°°ì´ì ììë ë¤ìê³¼ ê°ì´ ë°í ë ë°°ì´ì ë³µì¬ë©ëë¤:
- ê°ì²´ 참조(ë° ì¤ì ê°ì²´ê° ìë)ì ê²½ì°,
slice()ë ê°ì²´ 참조를 ì ë°°ì´ë¡ ë³µì¬í©ëë¤. ì본 ë°°ì´ê³¼ ì ë°°ì´ì 모ë ëì¼í ê°ì²´ë¥¼ 참조í©ëë¤. 참조 ë ê°ì²´ê° ë³ê²½ëë©´ ë³ê²½ ë´ì©ì ì ë°°ì´ê³¼ ìë ë°°ì´ ëª¨ëìì ë³¼ ì ììµëë¤. Stringë°Numberê°ì²´ê° ìë 문ìì´ê³¼ ì«ìì ê²½ì°slice()ë 문ìì´ê³¼ ì«ì를 ì ë°°ì´ì ë³µì¬í©ëë¤. í ë°°ì´ìì 문ìì´ì´ë ì«ì를 ë³ê²½í´ë ë¤ë¥¸ ë°°ì´ìë ìí¥ì ì£¼ì§ ììµëë¤.
ì ìì를 ë ë°°ì´ ì¤ íëì ì¶ê°í´ë ë¤ë¥¸ ë°°ì´ì ìí¥ì ë°ì§ ììµëë¤.
ìì
>기존 ë°°ì´ì ì¼ë¶ ë°í
let fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
let citrus = fruits.slice(1, 3);
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
slice ì¬ì©í기
ë¤ì ìì ìì slice()ë myCarìì newCarë¼ë ì ë°°ì´ì ë§ëëë¤. ë ê°ì§ 모ë myHonda ê°ì²´ì ëí 참조를 í¬í¨í©ëë¤. myHondaì ììì´ ì주ìì¼ë¡ ë³ê²½ëë©´ ë ë°°ì´ ëª¨ë ë³ê²½ ì¬íì ë°ìí©ëë¤.
// Using slice, create newCar from myCar.
let myHonda = { color: "red", wheels: 4, engine: { cylinders: 4, size: 2.2 } };
let myCar = [myHonda, 2, "cherry condition", "purchased 1997"];
let newCar = myCar.slice(0, 2);
// Display the values of myCar, newCar, and the color of myHonda
// referenced from both arrays.
console.log("myCar = " + JSON.stringify(myCar));
console.log("newCar = " + JSON.stringify(newCar));
console.log("myCar[0].color = " + myCar[0].color);
console.log("newCar[0].color = " + newCar[0].color);
// Change the color of myHonda.
myHonda.color = "purple";
console.log("The new color of my Honda is " + myHonda.color);
// Display the color of myHonda referenced from both arrays.
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() ë©ìë를 í¸ì¶íì¬ ë°°ì´í ê°ì²´ì ì½ë ì
ì ìë¡ì´ Arrayë¡ ë³íí ì ììµëë¤. ë¨ìí Function.prototype.bind()를 ì¬ì©í´ ê°ì²´ì slice()를 ë°ì¸ë© íë©´ ë©ëë¤. ëíì ì¸ "ë°°ì´í ê°ì²´"ì ììë í¨ì ë´ì argumentsì
ëë¤.
function list() {
return Array.prototype.slice.call(arguments);
}
let list1 = list(1, 2, 3); // [1, 2, 3]
Function.prototype.call() ë©ìë를 ì¬ì©í´ìë ë°ì¸ë©ì í ì ìì¼ë©°, Array.prototype.slice.call ëì ë ì§§ê² [].slice.callë¡ ìì±í ìë ììµëë¤.
ì무í¼, ë¤ìê³¼ ê°ì´ bind()를 ì¬ì©í´ ì¤ì¼ ì ììµëë¤.
let unboundSlice = Array.prototype.slice;
let slice = Function.prototype.call.bind(unboundSlice);
function list() {
return slice(arguments);
}
let list1 = list(1, 2, 3); // [1, 2, 3]
ëª ì¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-array.prototype.slice> |