스크립트
javascript slice / splice
고.니
2020. 12. 29. 23:06
반응형
const numbers = ['1', '2', '3', '4', '5'];
console.log(numbers.slice(2));
// expected output: Array ["3", "4", "5"]
console.log(numbers.slice(2, 4));
// expected output: Array ["3", "4"]
console.log(numbers.slice(1, 5));
// expected output: Array ["2", "3", "4", "5"]
numbers.splice(1, 0, '1.5');
// inserts at index 1
console.log(numbers);
// expected output: Array ["1", "1.5", "2", "3", "4", "5"]
numbers.splice(4, 1, '4.5');
// replaces 1 element at index 4
console.log(numbers);
// expected output: Array ["1", "1.5", "2", "3", "4.5", "5"]
numbers.splice(4, 2);
// removed "4,5", "5"
console.log(numbers);
// expected output: Array ["1", "1.5", "2", "3"]
https://prettycode.tistory.com
반응형