-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrays.js
125 lines (76 loc) · 2.85 KB
/
arrays.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
let a=[5,6,8];
console.log(a)
let cities = ['Hyderabad', 'Warangal', 'Mumbai', 'Delhi'];
console.log(cities);
cities[0]='Dubai';
console.log(cities);
cities =['Vizag', 'Agra','Chennai'];
console.log(cities);
const utensils = ['Fork', 'Knife', 'pan', 'Spork'];
utensils[3]='Spoon';
console.log(utensils);
// array methods *******************************************
// .length property in arrays
// length of array is given by variable_name.length
console.log(utensils.length);
// .push() method in array (adds element to last index )
cities.push('Kolkata');
console.log(cities);
//pop method ( pops out the last element of the array )
// .pop() returns the last element i.e removed element
let removed= cities.pop()
console.log ("The removed element is ", removed );
console.log("Array is ",cities);
// .shift() method
//removes the first element and returns the removed element
//If the length property is 0, undefined is returned
let shifted = cities.shift()
console.log( cities)
console.log(shifted)
// .unshift() method
//The unshift() method adds one or more elements to the beginning
//returns the new length of the array.
console.log(cities.unshift('Kolkata', 'Delhi'));
console.log(cities)
//The .slice() method returns a shallow copy of a portion of an array into a new array object
// selected from start to end (range of indices given as arguments)
//The original array will not be modified.
let topCities= cities.slice(1,3);
console.log(topCities);
// .splice() method
// splice(start)
// splice(start, deleteCount)
// splice(start, deleteCount, item1)
// splice(start, deleteCount, item1, item2, itemN)
cities.splice(1, 0, 'Ooty');
// inserts at index 1
console.log(cities);
cities.splice(3,1 , 'Kashmir')
console.log(cities)
//.find() method
//returns the first element in the provided array that satisfies the provided testing function
//return undefined if no element is found
const arr = [5, 12, 8, 130, 44];
const found = arr.find(element => element < 10);
console.log(found);
// .forEach() method
//executes a provided function once for each array element
arr.forEach(element => console.log(element));
// .map() method
// The map() method creates a new array populated with the results of calling a provided function
//on every element in the calling array
const arr2 = arr.map(x => x * 2);
console.log(arr2);
//.join() method ******************
const elements = ['Car', 'Bus', 'Auto'];
console.log(elements.join()); //joins with ','
// expected output: "Car,Bus,Auto"
console.log(elements.join(''));
// expected output: "CarBusAuto"
console.log(elements.join('-'));
// expected output: "Car-Bus-Auto"
//.filter() method
// creates a new array with all elements that pass the test
//implemented by the provided function
const result = cities.filter(word => word.length >= 5); //returns elements satisfying length>=5
console.log(result)