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}
1var events = [
2 {
3 userId: 1,
4 place: "Wormholes Allow Information to Escape Black Holes",
5 name: "Check out this recent discovery about workholes",
6 date: "2020-06-26T17:58:57.776Z",
7 id: 1
8 },
9 {
10 userId: 1,
11 place: "Wormholes Allow Information to Escape Black Holes",
12 name: "Check out this recent discovery about workholes",
13 date: "2020-06-26T17:58:57.776Z",
14 id: 2
15 },
16 {
17 userId: 1,
18 place: "Wormholes Allow Information to Escape Black Holes",
19 name: "Check out this recent discovery about workholes",
20 date: "2020-06-26T17:58:57.776Z",
21 id: 3
22 }
23];
24console.log(events[0].place);
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)