push
(스택1)
// push: add an item to the end (push는 맨 뒤에서부터 데이터를 추가한다.)
const fruits = ['apple', 'banana'];
fruits.push('stroberry', 'pitch');
console.log(fruits);
// ["apple", "banana", "stroberry", "pitch"]
pop
(스택2)
// pop: remove an item from the end (pop은 맨 뒤에서부터 데이터를 지운다.)
fruits.pop();
fruits.pop();
console.log(fruits);
// ["apple", "banana"]
unshift
(큐1)
// unshift: add an item to the beningging (unshift는 맨 앞에서 부터 데이터를 추가한다.)
fruits.unshift('stroberry', 'pitch');
console.log(fruits);
// ["stroberry", "pitch", "apple", "banana"]
shift
(큐2)
// shift: remove an item from the beningging (shift는 맨 앞에서 부터 데이터를 지운다.)
fruits.shift();
fruits.shift();
console.log(fruits);
// ["apple", "banana"]
※ shift, unshift are slower than pop, push (shift, unshift는 pop, push보다 느리다.)
splice
(부분적으로 지우기)
// splice: remove an item by index position (지정한 인덱스 위치부터 데이터를 지운다.)
const fruits = ['apple', 'banana'];
fruits.push('stroberry', 'pitch', 'lemon');
console.log(fruits); // ["apple", "banana", "stroberry", "pitch", "lemon"]
//fruits.splice(1); // 인덱스 1 부터 마지막까지 지운다.
fruits.splice(1, 1); // 인덱스 1 부터 1개만 지운다.
console.log(fruits); // ["apple", "stroberry", "pitch", "lemon"]
fruits.splice(1, 1, 'aa', 'bb'); // 인덱스 1 부터 1개의 데이터를 지우고, 그 부분에 해당 데이터를 추가한다.
console.log(fruits); // ["apple", "aa", "bb", "pitch", "lemon"]