-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray-literals.js
166 lines (109 loc) · 2.74 KB
/
array-literals.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* array */
//different types of array literals:-
/*
1.simple Array, array of numbers
const arr1 =[1,2,3,4,5];
2.non-homogeneous array literal
const arr2 =["one", 3, true];
3.array containing array(2Dimensional arrays)
const arr3 =[["one", "three"],[1,57,67]];
//nonhomogeneous array containing array
4.const arr4 =[
{name:"ak", no:34},[
//declare two objects
{name:"sos", subjects:"js"},{
name:"sos", subjects:"literals"
}
]
];
//array containing array with function
5.const arr5=[{
name:"jss", videos:o2o3
},[
//on 0 index of inner array
function(){
return "containing array";
},
//on 1 index of array
"three"
]
];
*/
/* access array elements */
//example
/*
const arr1 =[1,2,3,4,56,7];
console.log(arr1[5]);
*/
//example for 2D
/*
const arr3 =[["one", "three"],[1,57,67]];
console.log(arr3[0][0]);
*/
4
//example for objects
/*
const arr4 =[
{name:"ak", no:34},[
//declare two objects
{name:"sos", subjects:"js"},{
name:"os", subjects:"literals"
}
]
];
console.log(arr4[1][0].name);
console.log(arr4[0].no);
console.log(arr4[1][1].subjects);
*/
//example for functions
/*
const arr5=[{
name:"jss", videos:1213
},[
//on 0 index of inner array
function(){
return "containing array";
},
//on 1 index of array
"three"
]
];
console.log(arr5[0].name);
console.log(arr5[1][0]());
console.log(arr5[1][1]);
*/
/* ADD & REMOVE elements from array */
//Beginning refers to 0 index.
//End refers to arrayname.length -1.
//pop, push, shift and unshift(all these methods return new array)
//1. PUSH method(ADDS element at the end.)
//example
/*
const arr=[2,3,4];
console.log(arr.push(5,8));
console.log(arr);
//PUSH a string
console.log(arr.push("one"));
console.log(arr);
*/
//2.POP(removes the last element from the array)
//example
/*
const arr=[2,3,4];
console.log(arr.pop());
console.log(arr);
*/
//3. unshift(adds elemet at the beginning)
//example
/*
const arr=[2,3,4];
console.log(arr.unshift(5));
console.log(arr);
*/
//4.shift(remove element from the beginning)
//example
/*
const arr=[2,3,4];
console.log(arr.shift());
console.log(arr);
*/