An array is a variable where we can store multiple values of the same or different type in an organised list.
They are very handy when you have lots of information to store and organise.
Each item in a list has a number location starting at zero.
var arrayExample = [7,11,6,55,98,45,16,96,46];
The array index always starts at zero (0)
The length of this array is 9 not 8 because we start at zero
we can access the length of an array by arrayName.length
var cars = []; //This is an empty array
OR WITH VALUES
var cars = ["Saab", "Volvo", "BMW"];
cars[0] = "Opel";
Now cars =["Opel", "Volvo", "BMW"];
["Opel", "Volvo", "BMW"];
var x = cars[1];
x = "Volvo"
We use push()
cars.push("Toyota")
["Opel", "Volvo", "BMW", "Toyota"];
cars.splice(2,1) // go to index 2 and remove 1 item
["Opel", "Volvo", "Toyota"];
var x = cars.length; // The length property returns the number of elements
var y = cars.sort(); // The sort() method sorts arrays by the value inside each index
//create a array of values
var vals = [1,2,3,4,5];
var vals = []; //empty array
//print array to console log
console.log(vals);
//access an elemement in an array at index 3
console.log(vals[3]);
//add a new item/value to an array
vals.push(10); //adds value 10 to the array called vals
console.log(vals);
//delete last item from array
vals.pop();
console.log(vals);
//delete a specific item from array splice(index, how many)
vals.splice(0,1);
//first value is index start and second value is how many to delete = keep as 1
//loop through an array from first to last index using a for loop
//length returns the number of items in an array
for(var i=0; i<vals.length;i++){
console.log(vals[i]);
}
//search for an item in array using a function
var vals = [2,4,5,20,32,2,20,20];
search(3); //search for the number 3
search(20); //search for the number 20
//item is the number being searched for
function search(item){
var found = false;
for(var i=0; i<vals.length;i++){
if(vals[i]== item){
console.log(item + " was found");
found = true;
break;
}
}
if(found == false){
console.log(item + " was NOT found");
}
}
Processing arrays using loops
Objects can store multiple values about one object. An example is a person object. People have lots of properties like first, last names, age, DOB.
We declare a single object like this using curly brackets
var person = {firstName:"John", lastName:"Doe", age:46};
We can access the attributes of each object like this:
person.lastName; OR person["lastName"];
var people = [
{first: "ted", last: "smith" , age: 20}, //index 0
{first: "Sam", last: "apple" , age: 90} //index 1
];
people[0].first //would print "ted"
console.log("Hello " + people[0].first + " " + people[0].last) //output = Hello ted smith
These are declared inside the object declaration. The this command refers to the current object attribute.
fullName : function() {
return this.firstName + " " + this.lastName;
}
The method can be executed against an object by using the following notation:
objectName.methodName()
Array of objects - obects start and end with curly braces {} Properties are declared with a colon:
var cars = [
{car:"Ford",year:1999},
{car:"Holden",year:2009}
]
//access object in an array using the dot notation to access the property
console.log(cars[0].car);
console.log(cars[0].year);
//add an object to an array
cars.push({car:"Subaru",year:2020});
console.log(cars[2].car);
console.log(cars[2].year);
Sort an array of objects by a NUMBER property
employees.sort((a, b) => {
return a.age - b.age;
});
Sort an array of objects by a STRING property. NOTE: You must convert to lowercase as lowercase characters are lower then uppercase.
employees.sort((a, b) => {
let fa = a.firstName.toLowerCase(),
fb = b.firstName.toLowerCase();
if (fa < fb) {
return -1;
}
if (fa > fb) {
return 1;
}
return 0;
});
Sort an array of objects by date
employees.sort((a, b) => {
let da = new Date(a.joinedDate),
db = new Date(b.joinedDate);
return da - db;
});
Simple way to output objects
employees.forEach((e) => {
console.log(`${e.firstName} ${e.lastName} ${e.joinedDate}`);
});