-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17 arrays.js
More file actions
240 lines (157 loc) · 5.38 KB
/
17 arrays.js
File metadata and controls
240 lines (157 loc) · 5.38 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// ***************JavaScript Arrays************
// JavaScript arrays are used to store multiple values in a single variable.
const cars=["crola","volvo","BMW"];
// ******************What is an Array?***********************
// An array is a special variable, which can hold more than one value at a time.
// ********create array*************
// method 1
const cars1=["crola","volvo","BMW"];
// method 2
const car3=[
"crola",
"volvo",
"BMW"
];
// method 3
const car4=[];
car4[0]="rana";
car4[1]="volvo";
car4[2]="BMW";
console.log(cars);
/*
Using the JavaScript Keyword new
The following example also creates an Array, and assigns values to it:
*/
const cars4 = new Array("Saab", "Volvo", "BMW");
/*
The two examples above do exactly the same.
There is no need to use new Array().
For simplicity, readability and execution speed, use the first one (the array literal method).
*/
// ***********Accessing Array Elements************
// You access an array element by referring to the index number:
let x=cars[0];
console.log(x);//crola
// *************Changing an Array Element*************
cars[0]="rana";
let xx=cars[0];
console.log(xx);//rana
/*
//****************Access the Full Array*****************
With JavaScript, the full array can be accessed by referring to the array name:
*/
console.log(cars);
/*
//*****************Arrays are Objects***************
Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays.
But, JavaScript arrays are best described as arrays.
Arrays use numbers to access its "elements". In this example, person[0] returns John:
*/
/*
//******************The length Property*****************
The length property of an array returns the length of an array (the number of array elements).
*/
let y=cars[0];
// Accessing the Last Array Element
let a=cars[cars.length-1];
// *******Looping Array Elements********
for (let index = 0; index <cars.length; index++) {
console.log(cars[index]);
}
// ********You can also use the Array.forEach() function:*************
cars[0]="abobakar";
cars.forEach(show);
function show(value,index) {
console.log(cars[index]);
console.log(index ," = ",value);
}
/*
//********************Adding Array Elements*************************
The easiest way to add a new element to an array is using the push() method:
*/
cars.push("ranag");
console.log(cars[cars.length-1]);
//you cal also added an array
cars[cars.length]=100;
console.log(cars[cars.length-1]);
/*
WARNING !
Adding elements with high indexes can create undefined "holes" in an array:
*/
cars[9]=111;
console.log(cars);
/*
Associative Arrays
Many programming languages support arrays with named indexes.
Arrays with named indexes are called associative arrays (or hashes).
JavaScript does not support arrays with named indexes.
In JavaScript, arrays always use numbered indexes.
*/
//Example
const person = [];
person[0] = "John";
person[1] = "Doe";
person[2] = 46;
person.length; // Will return 3
person[0]; // Will return "John"
/*
WARNING !!
If you use named indexes, JavaScript will redefine the array to an object.
After that, some array methods and properties will produce incorrect results.
*/
//Example:
const person1 = [];
person1["firstName"] = "John";
person1["lastName"] = "Doe";
person1["age"] = 46;
person1.length; // Will return 0
person1[0]; // Will return undefined
/*
//**********The Difference Between Arrays and Objects*************
In JavaScript, arrays use numbered indexes.
In JavaScript, objects use named indexes.
Arrays are a special kind of objects, with numbered indexes.
//************When to Use Arrays. When to use Objects.**********
JavaScript does not support associative arrays.
You should use objects when you want the element names to be strings (text).
You should use arrays when you want the element names to be numbers.
//**********************Avoid new Array()*************
There is no need to use the JavaScript's built-in array constructor new Array().
Use [] instead.
These two different statements both create a new empty array named points:
*/
const points1 = new Array(); // Bad
const points2 = []; // Good
/*
//*********************How to Recognize an Array************************
A common question is: How do I know if a variable is an array?
The problem is that the JavaScript operator typeof returns "object":
*/
console.log(typeof cars);//object
// The typeof operator returns object because a JavaScript array is an object.
/*
************Solution 1:*********************
To solve this problem ECMAScript 5 defines a new method
Array.isArray():
*/
console.log(Array.isArray(cars));//true
/*
Solution 2:
The instanceof operator returns true if an object is created by a given constructor:
*/
console.log(cars instanceof Array);//true
console.log("array show");
cars.forEach(show);
function show(value) {
console.log(value);
}
cars.forEach(element => {
console.log(element)
});
//**** */ Array Destructuring in ES6
// The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
// ES6 has added a shorthand syntax for destructuring an array.
// The following example demonstrates how to unpack the elements of an array into distinct variables:
let arr=[1,2,3,4]
let [one,two,three]=arr;
console.log(one,two,three);