-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBASIC - OBJECTS - array objects.js
More file actions
48 lines (34 loc) · 1.33 KB
/
BASIC - OBJECTS - array objects.js
File metadata and controls
48 lines (34 loc) · 1.33 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
/*
!!BASIC - OBJECTS - ARRAY OBJECTS!!
*/
/*You can create arrays using a constructor too*/
var emptyArray = new Array();
/*We can add items to it*/
emptyArray[0] = 99;
/*Create array objects that have a specific size (3 elements)*/
var oddNumbers = new Array(3);
oddNumbers[0] = 1;
oddNumbers[1] = 3;
oddNumbers[2] = 5;
/*This reverses all the values in the array*/
oddNumbers.reverse();
/*The join method creates a string from the values in oddNumbers placing a
“ - ” between the values, and returns that string. So this returns the string
“5 - 3 - 1”.*/
var aString = oddNumbers.join(" - ");
/*The every method takes a function and tests each value of the array to see if
the function returns true or false when called on that value. If the function
returns true for all the array items, then the result of the every method is true.*/
var areAllOdd = oddNumbers.every( function(x) {
return ((x % 2) !== 0);
}
);
/*Create array - using the constructor*/
var items = new Array("a", "b", "c");
/*The constructor comes in handy when you need to create an array of a specific
size you determine at runtime, and then add items to it later*/
var n = getNumberOfWidgetsFromDatabase();
var widgets = new Array(n);
for(var i=0; i < n; i++) {
widgets[i] = getDatabaseRecord(i);
}