DEV Community

Dipayan Sanyal
Dipayan Sanyal

Posted on

Javascript -- Array and useful methods

join() method joins an arrow using a delimiter.

arr = [1,2,3,4];
var a = arr.join(' '); // a = 1234;
Enter fullscreen mode Exit fullscreen mode

join() method acts like the toString() except we can specify the delimiter.

*pop() deletes the last element in the array *
// returns the value that was popped.
*push() pushes an element in the last grid of the array *
// returns the new length of the array.

*shift() removes the first element in the array *
// returns the removed value.
*unshift() pushes an element in the first grid of the array *
// returns the new length of the array.

splice(position, number of elements, new elements);
splice helps in deleting elements of the array and sending out the same array not a copy.

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi");
//returns [ Banana,Orange,Lemon,Kiwiz,Apple,Mango ]

fruits.splice(2,2, "Pom", "Straw");
//returns [ Banana,Orange,Pom,Straw,Apple,Mango ]

fruits.splice(2,2);
//returns [ Banana, Orange, Apple, Mango ]
Enter fullscreen mode Exit fullscreen mode

The concat() method creates a new array by merging (concatenating) existing arrays:

var myGirls = ["Cecilie", "Lone"];
var myBoys = ["Emil", "Tobias", "Linus"];
var myChildren = myGirls.concat(myBoys); 
// concatenates (joins) myGirls and myBoys
Enter fullscreen mode Exit fullscreen mode

The concat() method also takes string as arguments. We can also use two arrays in the argument to concat three arrays.

The slice() method slices out a piece of an array into a new array.
This example slices out a part of an array starting from array element 1 ("Orange"):

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1);
// returns [“Orange”, “Lemon”, “Apple”, “Mango”];
Enter fullscreen mode Exit fullscreen mode

It is important to understand that this method returns a new array!
The slice() method can take 2 arguments like slice(1,3)
The method then selects elements from the start argument, and up to (but not including) the end argument.

var citrus = fruits.slice(1,3); // returns [“Orange”, “Lemon”]
Enter fullscreen mode Exit fullscreen mode

The sort() method sorts an array alphabetically. The reverse() method reverses the element in an array.
However, by default sort() method sorts values as strings. So, in order to sort numbers we have to use compare function. Let’s use an example.

var points = [20, 30, 10 4, 70, 44];
points.sort(); 
// returns [ 10,20,30,4,44,70 ] since the problem is 2 comes after 1 and so on.
Enter fullscreen mode Exit fullscreen mode

So using compare functions we can solve this problem.

var points = [20, 30, 10 4, 70, 44];
points.sort(function(a,b) {return a-b}); // ascending order.
// here two elements are pitted against each other. If the result //is negative, a is sorted before b, and if result is positive //then b is sorted before a.
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return 0.5 - Math.random()});
// sorts randomly
Enter fullscreen mode Exit fullscreen mode

Sorting object arrays.

var cars = [
    {type: ‘Volvo’, year:2016},
    {type: ‘BMW’, year:2018},
    {type: ‘Merc’, year: 2019}
];
cars.sort((a,b) => a.year - b.year);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)