the murmurous sea

JS ES1: slice() 본문

#dev/개념정리

JS ES1: slice()

lunacer 2020. 5. 4. 17:33

1. returns the selected elements in an array.
2. as a new array object.
3. a new array object is selected from begin to end.
4. end not included.
5. the begin and end represent the index of items in that array. 
6. The original array will not be modified.
7. can also be called to convert Array-like objects / collections to a new Array. (MDN doc)

 

[Syntax]

arr.slice([begin[, end]])
begin Optional An integer that specifies where to start the selection
    Zero-based index 
    At which to begin extraction.
    A negative index can be used, indicating an offset from the end of the sequence.
    If begin is undefined, slice begins from index 0.
    If begin is greater than the index range of the sequence,  an empty array is returned.
end Optional An integer that specifies where to end the selection.
    Zero-based index  
    Before which to end extraction. (slice extracts up to but not including end.)
    A negative index can be used, indicating an offset from the end of the sequence.
    If end is omitted,  all elements from the start position and to the end of the array will be selected(arr.length).
    If end is greater than the length of the sequence,  extracts through to the end of the sequence (arr.length).

 

[Return Value]

A new Array, containing the selected elements

 

[Example]

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var myBest = fruits.slice(-3, -1);
console.log (myBest);

//returns ["Lemon", "Apple"]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

https://www.w3schools.com/jsref/jsref_slice_array.asp

'#dev > 개념정리' 카테고리의 다른 글

DOM: Node.nodeValue  (0) 2020.05.05
DOM: nodeValue vs. textConent vs. innerText vs. innerHTML  (0) 2020.05.04
JS, ES1: splice()  (0) 2020.05.04
JS, ES1: split()  (0) 2020.05.04
JS: padStart() & padEnd()  (0) 2020.05.04
Comments