-
Notifications
You must be signed in to change notification settings - Fork 3
/
10.1 Array Methods in JS.html
117 lines (98 loc) · 5.66 KB
/
10.1 Array Methods in JS.html
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
114
115
116
117
<!DOCTYPE html>
<html lang="en">
<head><title>Array Methods in JS</title>
</head>
<body>
<!-- An array is a data structure, which can store a fixed-size collection of elements of the same data type.
->An array is a special variable, which can hold more than one value.
-> see program 10
visit: https://www.w3schools.com/js/js_array_methods.asp
-->
<script>
//JavaScript Array Methods:
const fruits=['Ornage',"Apple", "Mango","Banana"]
console.log("array: ",fruits)
//1-> Converting Arrays to Strings (toString() converts an array to a string )
console.log("Array to String using toString()");
console.log(fruits.toString());
//The join() method also joins all array elements into a string.
//It behaves just like toString(), but in addition you can specify the separator:
console.log("Array to String using join()");
console.log(fruits.join("*"));
/*
Automatic toString()
JavaScript automatically converts an array to a comma separated string when a primitive value is expected.
(In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods or properties)
This is always the case when you try to output an array.
These two examples will produce the same result:
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.toString();
is same as :
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
*/
//see pop() and push() in code 10
//Shifting is equivalent to popping, but working on the first element instead of the last.
//2->JavaScript Array shift()
//The shift() method removes the first array element and "shifts" all other elements to a lower index.
console.log("Array shift()")
var items=["Mouse","CPU","Camera","Keyboard"];
console.log(items.shift()); //shift() method returns the value that was "shifted out"/removed i.e mouse
//now CPU comes at zero
console.log("array after shift():",items); // o/p: ['CPU', 'Camera', 'Keyboard']
//3->JavaScript Array unshift()
//unshift() method adds a new element to an array (at the beginning), and "unshifts" older element
console.log("Array unshift()")
const fruits1 = ["Banana", "Orange", "Apple", "Mango"];
console.log("fruits array before unshift('Lemon')",fruits1)
console.log(fruits1.unshift());//o/p :4 //didn't add anything just returns array length which is here 4
console.log(fruits1.unshift("Lemon")); //o/p: 5 // adds element Lemon at index 0 and returns new array length which is now 5
//The unshift() method returns the new array length.
console.log("fruits array after unshift('Lemon')",fruits1)
//4-> Merging (Concatenating) Arrays
//The concat() method creates a new array by merging (concatenating) existing arrays:
//The concat() method does not change the existing arrays. It always returns a new array.
//The concat() method can take any number of array arguments:
console.log("Array concat()")
let arr1=["one","two"]
console.log("arr1: "+arr1);
let arr2=["three","four"]
console.log("arr2: "+arr2);
let Newarray=arr1.concat(arr2);
console.log("arr1 + arr2 = "+Newarray)
//we can merg also three array like this: var arr=arr1.concat(arr2,arr3);
//Merging an Array with Values:
console.log("Merging an Array with Values");
let arr=["This","is"];
console.log("arr: "+arr);
const newarray=arr.concat("Javascript");
console.log("arr:->",newarray);
// console.log(arr1+arr2); // can also join two arrays like this
//Splicing and Slicing Arrays already studied in program 10
//visit: https://www.javatpoint.com/javascript-array
//5-> JavaScript Array reverse() method
//The reverse() method is represented by the following syntax
console.log("Array reverse()")
const fruits2 = ["Banana", "Orange", "Apple", "Mango"];
console.log("Before Reverse",fruits2);
console.log("After Reverse: ",fruits2.reverse());
//6->JavaScript Array sort() method
//The JavaScript array sort() method is used to arrange the array elements in some order. By default, sort() method follows the ascending order
console.log("Array sort()")
var skills=["AngularJS","Node.js","JQuery","Bootstrap","Python","Java","C++"]
console.log("Before Sorting",skills);
console.log("After sorting: ",skills.sort());
//example 2:
var num=[8,9,5,4,7,6,2,3,1,0];
console.log(num.sort());
//How to recognize an array
//1-> using instanceof operator returns true when used on an array:
console.log(fruits instanceof Array) // o/p: true
// or 2-> using isArray() method
console.log(Array.isArray(fruits)) //o/p:true
//study more methods from : https://www.javatpoint.com/javascript-array
</script>
</body>
</html>