This is such a common task that some special loops were created, like the for...in loop:
const myPets = ['dog', 'cat', 'rat', 'snake'];
for(let i in myPets) {
console.log(myPets[i]);
}
Note: the for...in loop does not guarrente that the array will be processed in order.
for...in Loop on Objects
The for...in loop was designed for objects.
let car = {
make: "Ford",
model: "Mustang",
year: 1967
}
for(let prop in car){
console.log(prop, car[prop]);
}
for...of Loop
The for...of loop was designed specifically for arrays:
const myPets = ['dog', 'cat', 'rat', 'snake'];
for(let pet of myPets) {
console.log(pet);
}
Note: The for...of loop will access array elements in order.
Further Arrayology
We also have methods shift and unshift to add and remove from the beginning of an array
let todoList = ["homework"];
let task = "walk dog";
// push a task onto the end of the todo list
todoList.push(task);
// get and remove the first element
todoList.shift();
// add a task to the front of the list
todoList.unshift(task);
Arrays Continued …
indexOf finds the position of a value in an array
lastIndexOf begins searching at the end
Both of these can accept an optional second argument that indicates where to start searching
let list = [1,2,3,4,3,2,1];
list.indexOf(3); // 2
list.lastIndexOf(3); // 4
list.indexOf(3,3); // 4
Arrays Continued …
.slice() will create a new array with a segment of the array copied
takes a start and end index
When the end index is not given, it will go to the end of the array
Negative arguments will count from the end of the array
let list = [1,2,3,4,3,2,1];
list.slice(2, 4); // → [3, 4]
list.slice(4); // → [3, 2, 1]
let string = "abc";
string.length; // → 3
string.charAt(0); // → a
string[1]; // → b
Rest Parameters
Sometimes it is useful for functions to take any number of arguments.
function max(...numbers) {
let result = -Infinity;
for (let number of numbers) {
if (number > result) {
result = number;
}
}
return result;
}
console.log(max(4, 1, 9, -2));
// → 9
Rest Parameters Continued …
This is called a "Rest Parameter"
All the values are assigned to an Array with the given name
Spread Operator
Similarly, we can spread the values of an array into individual values, in an array:
let words = ["never", "fully"];
console.log(["will", ...words, "understand"]);
// → ["will", "never", "fully", "understand"]
Or in a function call:
let nums = [4,56,7,6,54,43,4,6,7,7];
let max = Math.max(...nums);
The Math Object
The Math object is a container to group a bunch of related functionality
There is only one Math object
It provides a namespace so that all these functions and values do not have to be global variables
Math.max(2, 4); // → 4
Math.min(2, 4); // → 2
Math.sqrt(4); // → 2
Math.PI; // → 3.141592653589793
Math.E; // → 2.718281828459045
// produce a random number between 0 and 1
Math.random(); // → 0.36993729369714856
// produce a whole random number between 1 and 10 inclusive
Math.floor(Math.random() * 10 + 1); // → 4
Destructuring
let person = {name: "Faraji", age: 23, gender: 'M'};
let {name} = person;
console.log(name);
// → Faraji
Works with arrays too:
let myPets = ['dog', 'cat', 'gerble', 'pig'];
let [firstPet, secondPet] = myPets;
console.log(firstPet, secondPet);
JSON
Often we want to store data to a file or send it to another computer
We can't send JavaScript arrays or objects as is
JSON is a text notation for JavaScript values
{
"make": "Ford",
"model": "Edge",
"year": 2012
}
JSON Continued …
JavaScript has functions JSON.stringify and JSON.parse to convert to and from JSON
JSON.parse - converts a JSON string to a JavaScript object
JSON.stringify - converts a JavaScript object to a JSON string
Summary
Objects and arrays (which are a specific kind of object) provide ways to group several values into a single value
Most values in JavaScript have properties, the exceptions being null and undefined
Properties are accessed using dot notation or square bracket notation
value.propName
value["propName"]
Summary Continued …
Objects tend to use names for their properties and store a fixed set of them
Arrays usually contain varying numbers of conceptually identical values and use numbers as the names of their properties (starting from 0)
Methods are functions that live in properties and usually act on the value they are a property of