In this article, we will be discussing the various functionalities offered by Javascript for Arrays. We will be starting with the basic Javascript functionalities along with some new features added in ES6.
So it’s almost summer and I guess fruits gonna be the best array name to start our experiments.
Note: Writing this article to serve the purpose of quick revision through most of the array features offered by Javascript.
Array Declaration
let fruits = [“mango”,”apple”];
console.log(fruits);
//Output :[ ‘mango’, ‘apple’ ]
Array length
let fruits = [“mango”,”apple”];
console.log(fruits.length);
//Output : 2
Adding element at the end of the array
fruits.push(“watermelon”);
console.log(fruits);
//Output : [ ‘mango’, ‘apple’, ‘watermelon’ ]
Deleting element from the end of the array
fruits.pop();
console.log(fruits);
//Output: [ ‘mango’, ‘apple’ ]
Deleting element from the start of the array
fruits.shift();
console.log(fruits);
//Output: [ ‘apple’ ]
Adding element at the start of the array
fruits.unshift(“banana”);
console.log(fruits);
//Output: [ ‘banana’, ‘apple’ ]
Slicing Array into parts
numbers = [12,31,23,76,3,9,45,2];
let fristTwo = numbers.slice(0,2);
console.log(fristTwo)
//Output: [12, 31]
Sort numeric array
numbers = [12,31,23,76,3,9,45,2];
console.log(“Original Array: “ + numbers)
let numberSort = numbers.sort((a,b) => { return a-b })
// return b-a for decending order
console.log(“Sorted Array: “ + numberSort)
/*Output:
Original Array: 12,31,23,76,3,9,45,2
Sorted Array: 2,3,9,12,23,31,45,76 */
Sort array alphabetically
fruits = [“mango”,”apple”,”watermelon”,”banana”]
let fruitSort = fruits.sort()
console.log(fruitSort)
//Output: [ 'apple', 'banana', 'mango', 'watermelon' ]
Reversing Array
fruits = [“mango”,”apple”,”watermelon”,”banana”]
fruits.reverse()
console.log(fruits);
//Output: [ 'banana', 'watermelon', 'apple', 'mango' ]
Array Iteration
Looping through array using forEach
fruits = [“mango”,”apple”,”watermelon”,”banana”]
fruits.forEach((fruit,index) => {
console.log(`${fruit} is at index ${index} in the array`)
})
/*Output:
mango is at index 0 in the array
apple is at index 1 in the array
watermelon is at index 2 in the array
banana is at index 3 in the array */
Filtering array according to the requirement
let numbers = [12,31,23,76,3,9,45,2];
let evennumber = numbers.filter(number => number%2==0)
console.log(‘Original Array: ‘ + numbers)
console.log(‘Filtered Array: ‘ + evennumber)
/*Output:
Original Array: 12,31,23,76,3,9,45,2
Filtered Array: 12,76,2 */
Modifying array according to requirement
let numbers = [12,31,23,76,3,9,45,2];
let timeTwo = numbers.map(number => number*2)
console.log(‘Original Array: ‘ + numbers)
console.log(‘Modified Array: ‘ + timeTwo)
/*Output:
Original Array: 12,31,23,76,3,9,45,2
Modified Array: 24,62,46,152,6,18,90,4 */
Array to String
fruits = [ "apple", "banana", "mango", "watermelon" ]
console.log(fruits.toString)
//Output: apple,banana,mango,watermelon
Concatenation of two array
let fruits = [“apple”,”mango”,”banana”]
let veggies = [“potato”,”onion”,”spinach”]
let food = fruits.concat(veggies);
console.log(food)
//Output: [ 'apple', 'mango', 'banana', 'potato', 'onion', 'spinach' ]
To be updated soon with few more functionalities of Array
Top comments (0)