-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11-sliceMethod.js
More file actions
109 lines (82 loc) · 2.71 KB
/
11-sliceMethod.js
File metadata and controls
109 lines (82 loc) · 2.71 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*JavaScript Array slice() Method
The Array slice() method returns selected elements in an array as a new array.
It selects from a given start, up to a (not inclusive) given end. This method
does not change the original array, enabling non-destructive extraction of array segments.
Syntax:
arr.slice(begin, end);
Parameters:
begin: This parameter defines the starting index from where the portion is to be extracted.
If this argument is missing then the method takes begin as 0 as it is the default start value.
end: Parameter specifying the end index for extracting a portion from an array, defaulting
to array length if undefined, adjusting for exceeding length.
Return value:
This method returns a new array containing some portion of the original array. */
/*Example 1: Extracting elements between two indexes
Here, the slice() method extracts the array from the given array starting from index 2
and including all the elements less than index 4.*/
function func() {
// Original Array
let arr = [23, 56, 87, 32, 75, 13];
// Extracted array
let new_arr = arr.slice(2, 4);
console.log(arr);
console.log(new_arr);
}
func();
console.log("---------------------------------------------------------")
// Output
// [ 23, 56, 87, 32, 75, 13 ]
// [ 87, 32 ]
/*Example 2: Passing no arguments
Here, the slice() method extracts the entire array from the given string and returns it as the answer,
Since no arguments were passed to it.*/
function func1() {
//Original Array
let arr = [23, 56, 87, 32, 75, 13];
//Extracted array
let new_arr = arr.slice();
console.log(arr);
console.log(new_arr);
}
func1();
console.log("---------------------------------------------------------")
// Output
// [ 23, 56, 87, 32, 75, 13 ]
// [ 23, 56, 87, 32, 75, 13 ]
/*Example 3: Extracting array from index 2
In this example, the slice() method extracts the array starting from index 2 till the
end of the array and returns it as the answer.*/
function func2() {
//Original Array
let arr = [23, 56, 87, 32, 75, 13];
//Extracted array
let new_arr = arr.slice(2);
console.log(arr);
console.log(new_arr);
}
func2();
console.log("---------------------------------------------------------")
// Output
// [ 23, 56, 87, 32, 75, 13 ]
// [ 87, 32, 75, 13 ]
/*Example 4: Slicing the nested Array
In this example, the slice() method extracts the elements from the nested array and
returns it as the answer.*/
function func3() {
// Original Array
let arr = [23, [87, 32, 75, 27,3,10,18 ,13]];
// Extracted array
let new_arr = arr[1].slice(2, 4);
console.log(arr);
console.log(new_arr);
}
func3();
/*Output
[
23,
[
87, 32, 75, 27,
3, 10, 18, 13
]
]
[ 75, 27 ]*/