1+ const numbers = [ 1 , 2 ] ;
2+
3+ //**Adding new element: End */
4+ numbers . push ( 3 , 4 ) ;
5+ numbers . push ( 5 ) ;
6+ console . log ( numbers ) ;
7+
8+ //**Adding new element: Begining */
9+ numbers . unshift ( 0 ) ;
10+ numbers . unshift ( 0 , 1 ) ;
11+ console . log ( numbers ) ;
12+
13+ //**Adding new element: Middle*/
14+ numbers . splice ( 2 , 0 , 'a' , 'b' ) ;
15+ console . log ( numbers ) ;
16+ numbers . splice ( 2 , 1 , 'c' , 'd' ) ;
17+ console . log ( numbers ) ;
18+ /*******************************/
19+
20+ //**Finding element: Primitives */
21+
22+ console . log ( numbers . indexOf ( 'a' ) ) ;
23+ console . log ( numbers . indexOf ( 1 ) ) ;
24+ console . log ( numbers . indexOf ( 1 , 2 ) ) ;
25+ console . log ( numbers . indexOf ( 'c' ) ) ;
26+
27+ console . log ( numbers . lastIndexOf ( 0 ) ) ;
28+ console . log ( numbers . indexOf ( 5 ) !== - 1 ) ;
29+ console . log ( numbers . includes ( 1 ) ) ;
30+ console . log ( numbers . includes ( 'a' ) ) ;
31+
32+ //**Finding element: References */
33+
34+ const courses = [ { id :1 , name :'a' } , { id :1 , name :'b' } , { id :1 , name :'c' } ] ;
35+ console . log ( courses . find ( function ( course ) { return course . name === 'a' } ) ) ;
36+ console . log ( courses . findIndex ( function ( course ) { return course . name === 'a' } ) ) ;
37+ console . log ( courses . find ( course => course . name === 'a' ) ) ;
38+ console . log ( courses . findIndex ( course => course . name === 'a' ) ) ;
39+
40+ //**Removing element : End*/
41+ console . log ( numbers . pop ( ) ) ;
42+ //**Removing element: Begining */
43+ console . log ( numbers . shift ( ) ) ;
44+ //**Removing element: Middle */
45+ console . log ( numbers . splice ( 2 , 1 ) ) ;
46+ console . log ( numbers ) ;
47+
48+ //**Empty an array*/
49+ let numbers1 = [ 1 , 2 , 3 ] ;
50+ let another = numbers1 ;
51+ let another1 = numbers1 ;
52+ let another2 = numbers1 ;
53+ numbers1 = [ ] ;
54+ console . log ( numbers1 ) ;
55+ console . log ( another . length = 0 ) ;
56+ console . log ( another ) ;
57+ console . log ( another1 . splice ( 0 , another1 . length ) ) ;
58+
59+ while ( another2 . length > 0 )
60+ another2 . pop ( ) ;
61+ console . log ( another2 ) ;
62+
63+ //**Combining and slicing an arrays */
64+
65+ const first = [ 1 , 2 , 3 ] ;
66+ const second = [ 4 , 5 , 6 ] ;
67+ const third = [ { id :1 } ] ;
68+
69+ const combined = first . concat ( second ) . concat ( third ) ;
70+ console . log ( combined ) ;
71+ console . log ( combined . slice ( 2 , 4 ) ) ;
72+ console . log ( combined . slice ( 6 , 7 ) ) ;
73+
74+ //**Spread operator */
75+ const concatnated = [ ...first , ...second ] ;
76+ console . log ( concatnated ) ;
77+ console . log ( concatnated . slice ( ) ) ;
78+
79+ //**Iterating an array */
80+ concatnated . forEach ( i => console . log ( i ) ) ;
81+
82+ concatnated . forEach ( ( i , index ) => console . log ( i , index ) ) ;
83+
84+ for ( let number of numbers ) console . log ( number ) ;
85+
86+ //**Joining an array */
87+ console . log ( concatnated . join ( ',' ) ) ;
88+
89+ //**Spliting as an array */
90+ const str = "This is a String" ;
91+ const splitArray = str . split ( ' ' ) ;
92+ console . log ( splitArray ) ;
93+
94+ console . log ( splitArray . join ( '-' ) ) ;
95+
96+ //**Sorting as an array */
97+ const numberArray = [ 3 , 1 , 2 ] ;
98+ numberArray . sort ( ) ;
99+ console . log ( numberArray ) ;
100+ numberArray . reverse ( ) ;
101+ console . log ( numberArray ) ;
102+
103+ const courses1 = [ { id :1 , name :'Node JS' } , { id :2 , name :'Java' } ] ;
104+ console . log ( courses1 . sort ( ( x , y ) => {
105+ const a = x . name . toUpperCase ( ) ;
106+ const b = y . name . toUpperCase ( ) ;
107+ if ( a < b ) return - 1 ;
108+ if ( a > b ) return 1 ;
109+ if ( a == b ) return 0 ;
110+ } ) ) ;
111+
112+ //**Testing elements of an array */
113+ const everyValues = [ 1 , 2 , 3 , 4 , 5 ] ;
114+ const someValues = [ 1 , 2 , 3 , - 4 , 5 ] ;
115+
116+ const allPositives = everyValues . every ( value => value >= 0 ) ;
117+ const somePositives = someValues . some ( value => value >= 0 ) ;
118+
119+ console . log ( allPositives ) ;
120+ console . log ( somePositives ) ;
121+
122+ console . log ( someValues . every ( value => value >= 0 ) ) ;
123+ console . log ( everyValues . some ( value => value >= 0 ) ) ;
124+
125+ //**Filtering an array */
126+
127+ const filteredArray = someValues . filter ( value => value >= 0 ) ;
128+ console . log ( filteredArray ) ;
129+
130+ //**Mapping an array */
131+
132+ const mappedArray = filteredArray . map ( value => '<li>' + value + '</li>' ) ;
133+ const html = mappedArray . join ( ' ' ) ;
134+ console . log ( html ) ;
135+
136+ const mappedArray1 = someValues
137+ . filter ( value => value >= 0 )
138+ . map ( value => ( { value : value } ) )
139+ . filter ( obj => obj . value > 1 )
140+ . map ( obj => obj . value ) ;
141+ console . log ( mappedArray1 ) ;
142+
143+ //**Reducing an array */
144+ const result = someValues . reduce ( ( accumlator , currentValue ) => accumlator + currentValue , 0 ) ;
145+ console . log ( result ) ;
0 commit comments