|
| 1 | +// Deletion ways in array |
| 2 | + |
| 3 | +var arr = [10,20,1,2,90,33, 88]; |
| 4 | +undefined |
| 5 | + |
| 6 | + |
| 7 | +arr.pop(); // delete a element from the last |
| 8 | +88 |
| 9 | +arr; |
| 10 | +(6) [10, 20, 1, 2, 90, 33] // result |
| 11 | + |
| 12 | + |
| 13 | +arr.shift(); // delete a element from the starting |
| 14 | +10 |
| 15 | +arr.splice(1, 1); // From Index 1 delete only one element |
| 16 | +[1] |
| 17 | +arr; |
| 18 | +(4) [20, 2, 90, 33] // result |
| 19 | + |
| 20 | + |
| 21 | +arr.length=0; // this will remove all the elemets from the array as length is 0 now. |
| 22 | +0 |
| 23 | +arr; |
| 24 | +[] |
| 25 | + |
| 26 | + |
| 27 | +var arr = [10,20,1,2,90,33, 88]; |
| 28 | +undefined |
| 29 | +arr.length = 4; // this will remove all the elemets from the array except staring given length. |
| 30 | +4 |
| 31 | +arr; |
| 32 | +(4) [10, 20, 1, 2] // result |
| 33 | + |
| 34 | + |
| 35 | +var arr = [10,20,1,2,90,33, 88]; |
| 36 | +undefined |
| 37 | +arr = arr.filter(e=>e<30); // this will remove all the elements which are greater than 30. // DECLARATIVE PROGRAMING |
| 38 | +(4) [10, 20, 1, 2] |
| 39 | +arr; |
| 40 | +(4) [10, 20, 1, 2] |
| 41 | + |
| 42 | + |
| 43 | +var arr = [10,20,1,2,90,33, 88]; |
| 44 | +undefined |
| 45 | +arr = arr.reduce((temp, e)=>{ // this will remove all the elements which are greater than 30. // IMPARATIVE PROGRAMING |
| 46 | + if(e<30){ |
| 47 | + temp.push(e); |
| 48 | + } |
| 49 | + return temp; |
| 50 | +}, []); |
| 51 | +(4) [10, 20, 1, 2] |
| 52 | +arr; |
| 53 | +(4) [10, 20, 1, 2] |
| 54 | + |
| 55 | +arr[0] = 10000; // Updation in array |
| 56 | +10000 |
| 57 | +arr; |
| 58 | +(4) [10000, 20, 1, 2] |
0 commit comments