-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array.js
76 lines (59 loc) · 2.62 KB
/
Array.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
let Fruit = ['Apple', 'Banana', 'Mango', 'Guava', 'Pineapple', 'Graps'];
/*
for(let i = 0 ; i < Fruit.length ; i++)
console.log(Fruit[i]);
let array = ['Arjun', 1, true, {name : 'arjun'}, function(){console.log('hello')}]
for(let i = 0 ; i < array.length-2 ; i++)
console.log(array.at(i));
console.log(array.at(-2).name);
array.at(-1)();
*/
// Methods of push/pop, and shift/unshift
// console.log("------------- POP ------------")
// console.log(Fruit);
// console.log(Fruit.pop()); // it delete element from last
// console.log(Fruit);
// console.log("------------- PUSH ------------")
// Fruit.push('Graps'); // it add element at last
// console.log(Fruit);
// console.log("------------- SHIFT ------------")
// console.log(Fruit.shift()) // it remove a element from begining
// console.log(Fruit);
// console.log("------------- UNSHIFT ------------")
// Fruit.unshift('Apple');
// console.log(Fruit); // it add element at first position
// console.log("------------- SPLICE ------------")
// The arr.splice method is a swiss army knife for arrays. It can do everything: insert, remove and replace elements.
// let arr = ['I', 'Will', 'go', 'home'];
// console.log(arr);
// delete arr[3]; // it remove that element but length is still same
// arr.splice(3,1); // from index 3 delete 1 element
// console.log(arr);
// arr.splice(3, 1, "Offce"); // it replace index 3 1 element
// console.log(arr);
// arr.splice(3,0,"office","and"); // it insert value at position
// console.log(arr);
// console.log("------------- SLICE ------------")
// // The method arr.slice is much simpler than similar-looking arr.splice.
// let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
// console.log(arr.slice(2, 5)); // copy from 2 to 5
// console.log(arr.slice(-4)); // copyt from -4 till end
// // It returns a new array copying to it all items from index start to end (not including end).
// // Both start and end can be negative, in that case position from array end is assumed.
// console.log("------------- CONCAT ------------");
// // The method arr.concat creates a new array that includes values from other arrays and additional items.
// let arr = [1,2];
// // console.log(arr.concat(3,4,5,6));
// let arr1 = {
// 0: "HI",
// 1: "Hello",
// length:5
// }
// console.log(arr.concat(arr1));
// console.log("------------- Iterate (forEach) ------------");
// // The arr.forEach method allows to run a function for every element of the array.
// Fruit.forEach((item, index, array)=>{
// console.log(`${item} at index = ${index} in array ${array}`)
// });
console.log("------------- Searching in Array ------------");
// Now let's cover method that search in array