|
| 1 | +// = = = Array Methods = = = |
| 2 | +console.log(" - - - - - - - - - - Array Methods - - - - - - - - - - "); |
| 3 | + |
| 4 | +let dryFruits = ["Almonds", "Apricot", "Pistachio", "Raisins", "Walnut"]; |
| 5 | +let fruits = ['Apple', 'Banana', 'Orange', 'Papaya', 'Grape', 'Mango']; |
| 6 | + |
| 7 | +console.log(dryFruits.length); //Find array length |
| 8 | + |
| 9 | +let dryFruitsString = dryFruits.toString(); //Converts an array to a string of (comma separated) array values |
| 10 | +console.log(dryFruitsString); |
| 11 | + |
| 12 | +let dryFruitsJoin = dryFruits.join(" - "); //Joins all array elements into a string |
| 13 | +console.log(dryFruitsJoin); |
| 14 | + |
| 15 | +let dryFruitsSplit = dryFruitsJoin.split(" - "); //Split a string to an array |
| 16 | +console.log(dryFruitsSplit); |
| 17 | + |
| 18 | +let removeLast = dryFruits.pop(); //Removes the last element from an array |
| 19 | +console.log(removeLast); //Returns the value that was "popped out" |
| 20 | + |
| 21 | +let addLast = dryFruits.push("Peanuts"); //Adds a new element to an array (at the end) |
| 22 | +console.log(addLast); //Return the new array length |
| 23 | + |
| 24 | +let removeFirst = dryFruits.shift(); //Removes the first element from and array |
| 25 | +console.log(removeFirst); //Returns the value that was "shifted out" |
| 26 | + |
| 27 | +let addFirst = dryFruits.unshift("Saffron"); //Adds a new element to an array (at the beginning) |
| 28 | +console.log(addFirst); //Returns the new array length |
| 29 | + |
| 30 | +console.log(dryFruits); //Print Array |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +console.log(" - - - - - - - - - - Array.isArray() - - - - - - - - - - "); |
| 35 | +console.log(Array.isArray(dryFruits)); |
| 36 | + |
| 37 | +console.log(" - - - - - - - - - - indexOf() - - - - - - - - - - "); |
| 38 | +console.log(dryFruits.indexOf('Raisins')); |
| 39 | + |
| 40 | +console.log(" - - - - - - - - - - concat() - - - - - - - - - - "); |
| 41 | +let mixFruits = dryFruits.concat(fruits); |
| 42 | +console.log("Mix Fruits (Concatenation Array) : ", mixFruits); |
| 43 | +console.log("Dry Fruits : ", dryFruits); |
| 44 | +console.log("Fruits : ", fruits); |
| 45 | + |
| 46 | +console.log(" - - - - - - - - - - sort() - - - - - - - - - - "); |
| 47 | +let mixFruitsSort = mixFruits.sort(); |
| 48 | +console.log("Mix Fruits (Sort Array) : ", mixFruitsSort); |
| 49 | +console.log("Mix Fruits : ", mixFruits); |
| 50 | + |
| 51 | +console.log(" - - - - - - - - - - reverse() - - - - - - - - - - "); |
| 52 | +let mixFruitsReverse = mixFruits.reverse(); |
| 53 | +console.log("Mix Fruits (Reverse Array) : ", mixFruitsReverse); |
| 54 | +console.log("Mix Fruits : ", mixFruits); |
| 55 | + |
| 56 | +console.log(" - - - - - - - - - - splice() - - - - - - - - - - "); |
| 57 | +let spliceFruits = mixFruits.splice(2, 5); |
| 58 | +console.log("Mix Fruits (Splice Array) : ", spliceFruits); |
| 59 | +console.log("Mix Fruits : ", mixFruits); |
| 60 | + |
0 commit comments