Skip to content

Commit c40b57d

Browse files
committed
Add Content of Array
1 parent 4481378 commit c40b57d

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

02_Basic/01_Array.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Array
2+
3+
const myArr = [0,2,3,4,5,6]
4+
5+
console.log(myArr[0]); // Zeroth based indexing output => 0
6+
7+
const myHeros= ['ankit','kumar','singh']
8+
9+
const myArr2 = new Array(1,2,3,4,5) // Another Way of intialise an array
10+
console.log(myArr2)
11+
12+
console.log(myArr[2]) // output => 3
13+
14+
15+
// Array Methods
16+
17+
myArr.push(6) // push element in array after last index of array
18+
console.log(myArr) // output => [0, 2, 3, 4,5, 6, 6]
19+
20+
myArr.push(7)
21+
console.log(myArr) // output => [0, 2, 3, 4,5, 6, 6, 7]
22+
23+
myArr.pop() // pop the last index element of an array
24+
console.log(myArr) // output => [0, 2, 3, 4,5, 6, 6]
25+
26+
myArr.unshift(9); // right shift of every element of an array and push element at starting of index
27+
console.log(myArr) // output => [9,0, 2, 3, 4,5, 6, 6]
28+
29+
myArr.shift(9); // left shift of every element of an array and pop element at starting of index
30+
console.log(myArr) // output => [0, 2, 3, 4,5, 6, 6]
31+
32+
console.log(myArr.includes(9)) //output => false
33+
console.log(myArr.indexOf(3)) //output => 2 if not present return -1
34+
35+
const newArr = myArr.join() //Adds all the elements of an array into a string, separated by the specified separator string
36+
37+
console.log(myArr); // output => [0, 2, 3, 4,5, 6, 6]
38+
console.log(newArr); // output => 0, 2, 3, 4,5, 6, 6
39+
40+
//slice ,splice
41+
42+
console.log("A",myArr); // output => A [0, 2, 3, 4,5, 6, 6]
43+
44+
const myn1 = myArr.slice(1,3) // Returns a copy of a section of an array
45+
console.log(myn1) // output => [ 2, 3 ]
46+
47+
console.log("B",myArr); // output => B [0, 2, 3, 4,5, 6, 6]
48+
49+
const myn2 = myArr.splice(1,3) // Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
50+
console.log(myn2); // output => [ 2, 3, 4 ]
51+
52+
console.log("C",myArr); // output => C [ 0, 5, 6, 6 ]
53+
54+
55+

02_Basic/02_array.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const marvel_heros = ['thor','ironman','spiderman']
2+
3+
const dc_heros = ['superman','flash',"batman"]
4+
5+
// marvel_heros.push(dc_heros) // push the array as single element here
6+
7+
console.log(marvel_heros); // output => [ 'thor', 'ironman', 'sipderman', [ 'superman', 'flash', 'batman' ] ]
8+
// console.log(marvel_heros[3][1]); // output => flash
9+
10+
const all_heros = marvel_heros.concat(dc_heros) // Combines two or more arrays. This method returns a new array without modifying any existing arrays.
11+
console.log(all_heros) // output => [ 'thor', 'ironman', 'sipderman', 'superman', 'flash', 'batman' ]
12+
13+
//spread operator
14+
15+
const all_new_heros = [...marvel_heros,...dc_heros] // spread
16+
console.log(all_new_heros) // output => [ 'thor', 'ironman', 'sipderman', 'superman', 'flash', 'batman' ]
17+
18+
const another_array = [1,2,3,[4,5,6],7,[6,7,[4,5]]]
19+
20+
const real_another_array = another_array.flat(Infinity)
21+
console.log(real_another_array) // output => [1, 2, 3, 4, 5, 6, 7, 6, 7, 4,5]
22+
23+
24+
25+
console.log(Array.isArray("Ankit")) // to chech array or not // output => false
26+
console.log(Array.from("Ankit")) // used to convert into array // output => [ 'A', 'n', 'k', 'i', 't' ]
27+
console.log(Array.from({name : "hitesh"})) // output => []
28+
29+
30+
31+
let score1 =100
32+
let score2 = 200
33+
let score3 = 300
34+
35+
console.log(Array.of(score1,score2,score1)) // output =>[ 100, 200, 100 ]

0 commit comments

Comments
 (0)