JavaScript

Different Ways To Remove An Elements From An Array In JavaScript.

Description.

Array in JavaScript is used to store multiple values in a single variable. It is just like objects which has methods to perform traversal and mutation operations. Neither the length of a JavaScript array nor the types of its elements are fixed.

Create An Array.

Array literal is the easiest way to create an array in JavaScript.

var color= ['red', 'blue', 'green','yellow']; 

Another way is to create an array in JavaScript using Keyword new.

var color= new Array('red', 'blue', 'green','yellow'); 

Removing Elements By Using JavaScript Array Length Property.

Javascript array elements can be removed by using array length property. An element greater than or equal to the given length will be removed.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var array = [1, 2, 3, 4, 5, 6];
array.length = 2;
console.log( array ); // [1, 2]
var array = [1, 2, 3, 4, 5, 6]; array.length = 2; console.log( array ); // [1, 2]
var array = [1, 2, 3, 4, 5, 6];
array.length = 2;  
console.log( array ); // [1, 2]
 

Removing Elements By Using pop method.

The pop method will delete the last element of an array.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var array = [1, 2, 3, 4, 5, 6];
array.pop()
console.log( array);
var array = [1, 2, 3, 4, 5, 6]; array.pop() console.log( array);
var array = [1, 2, 3, 4, 5, 6]; 
array.pop()
console.log( array);

Removing Elements By Using shift method.

 This method is used to remove the first element of the array and reducing the size of original array by 1.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var array = [1, 2, 3, 4, 5, 6];
array.shift()
console.log(array);
var array = [1, 2, 3, 4, 5, 6]; array.shift() console.log(array);
var array = [1, 2, 3, 4, 5, 6]; 
array.shift()
console.log(array); 

Removing Elements By Using splice method.

This method is used to modify the contents of an array by removing the existing elements and/or by adding new elements. To remove elements by splice() method you can specify the elements in different ways.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var array = [1, 2, 3, 4, 5, 6];
array.splice(2,2)
console.log(array);
var array = [1, 2, 3, 4, 5, 6]; array.splice(2,2) console.log(array);
var array = [1, 2, 3, 4, 5, 6]; 
array.splice(2,2)
console.log(array);

Removing Elements By Using filter method.

Unlike the splice method, filter creates a new array. filter() does not mutate the array on which it is called, but returns a new array.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var array = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
var result = array .filter(word => word.length > 6);
alert(result);
var array = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']; var result = array .filter(word => word.length > 6); alert(result);
var array = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
var result = array .filter(word => word.length > 6);
alert(result);

Related Articles

Leave a Reply

Back to top button