1//Creating a list
2var fruits = ["Apple", "Banana", "Cherry"];
3
4//Creating an empty list
5var books = [];
6
7//Getting element of list
8//Lists are ordered using indexes
9//The first element in an list has an index of 0,
10//the second an index of 1,
11//the third an index of 2,
12//and so on.
13console.log(fruits[0]) //This prints the first element
14//in the list fruits, which in this case is "Apple"
15
16//Adding to lists
17fruits.push("Orange") //This will add orange to the end of the list "fruits"
18fruits[1]="Blueberry" //The second element will be changed to Blueberry
19
20//Removing from lists
21fruits.splice(0, 1) //This will remove the first element,
22//in this case Apple, from the list
23fruits.splice(0, 2) //Will remove first and second element
24//Splice goes from index of first argument to index of second argument (excluding second argument)
1//create an array like so:
2var colors = ["red","blue","green"];
3
4//you can loop through an array like this:
5for (var i = 0; i < colors.length; i++) {
6 console.log(colors[i]);
7}
1//Their are no lists in javascript, they are called arays,wich are basically the same thing
2
3var fruits = ['Apple', 'Banana'];
4
5console.log(fruits.length);
6// 2
7var first = fruits[0];
8// Apple
9
10var last = fruits[fruits.length - 1];
11// Banana
12
13fruits.forEach(function(item, index, array) {
14 console.log(item, index);
15});
16// Apple 0
17// Banana 1
18
19var newLength = fruits.push('Orange');
20// ["Apple", "Banana", "Orange"]
21
22var last = fruits.pop(); // deletes orange
23// ["Apple", "Banana"];
24
25var first = fruits.shift(); // supprime Apple (au début)
26// ["Banana"];
27
28var newLength = fruits.unshift('Strawberry') // adds to the start
29// ["Strawberry", "Banana"];
30
31fruits.push('Mango');
32// ["Strawberry", "Banana", "Mango"]
33
34var pos = fruits.indexOf('Banana');
35// 1
36
37var removedItem = fruits.splice(pos, 1); // deletes on element at the position pos
38//removes one element at the position pos
39// ["Strawberry", "Mango"]
40
41
42
43
44var vegetables = ['Cabbage', 'Turnip', 'Radish', 'Carrot'];
45console.log(vegetables);
46// ["Cabbage", "Turnip", "Radish", "Carrot"]
47
48var pos = 1, n = 2;
49
50var removedItems = vegetables.splice(pos, n);
51// n defines the number of element to delete starting from pos
52
53console.log(vegetables);
54// ["Cabbage", "Carrot"]
55console.log(removedItems);
56// ["Turnip", "Radish"] (splice returns list of deleted objects)
57
1var familly = []; //no familly :(
2var familly = new Array() // uncommon
3var familly = [Talel, Wafa, Eline, True, 4];
4console.log(familly[0]) //Talel
5familly[0] + "<3" + familly[1] //Talel <3 Wafa
6familly[3] = "Amir"; //New coming member in the family
1special_dates = [];
2special_dates.push(new Date('2021/02/12').getTime());
3special_dates.push(new Date('2021/02/13').getTime());
4special_dates.push(new Date('2021/02/14').getTime());
5
6
7for (var i = 0; i < special_dates.length; i++) {
8 console.log(special_dates[i]) ;
9}
10