-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05_spliceToRemoveItems.js
32 lines (27 loc) · 1018 Bytes
/
05_spliceToRemoveItems.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Remove Items Using splice()
/* The splice method will remove any number
of consecutive elements from anywhere within
the array.
The splice() takes up to three parameters: an index
for where to start removing items, and the number of
items to remove are the first two.*/
let array = ['today', 'was', 'not', 'so', 'great'];
array.splice(2, 2);
console.log(array); // ['today', 'was', 'great']
// The splice() method also returns another array:
array = ['I', 'am', 'feeling', 'really', 'happy'];
let newArray = array.splice(3, 2);
console.log(array, newArray); // ['I', 'am', 'feeling'] ['really', 'happy']
/* We've initialized an array arr. Use splice() to remove
elements from arr, so that it only contains elements that
sum to the value of 10. */
const arr = [2, 4, 5, 1, 7, 5, 2, 1];
// Only change code below this line
arr.splice(0, 2);
arr.splice(1, 2);
arr.splice(2, 2);
// for (let i = 0; i < 3; i++) {
// arr.splice(i, 2);
// }
// Only change code above this line
console.log(arr); // [5, 5]