Warning: session_start(): open(/tmp/sess_bdd578723fb7c990ca3b4070bd58b5bf, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
Remove Element(s) From An Array In JavaScript - LearnShareIT

Remove Element(s) From An Array In JavaScript

Remove Element(s) From An Array In JavaScript

This article will show you how to remove element(s) from an array in JavaScript, which is probably one of the most commonly used operations in practice when working with JavaScript. Let’s jump right in.

How to remove element(s) from an array in JavaScript

We have many ways to remove element(s) from an array in JavaScript but I will teach you the easiest way. So, please read this article to the end to find out.

Using pop() or shift() method

If you want to remove the last element of an array, you can use the pop() method. This function returns the last element and removes the last element in the array. Besides that, you can also use the shift() method to remove the first element of an array.

Syntax:

Remove last element:

pop()

Remove first element:

shift()

Parameters: No parameter.

Return value: The last or the first element removed of the array.

Example:

// Initialize an array
var arr1 = ["Learn", "Share", "IT"];

// Remove last element
arr1.pop();

// Show array has been deleted the last element
console.log("New array after removed the last element:", arr1);

// Initialize another array
var arr2 = ["5", "12", "532"];

// Remove fisrt element
arr2.shift();

// Show array has been deleted the first element
console.log("New array after remove the first element:", arr2);

Output:

New array after remove the last element: ["Learn", "Share"]
New array after remove the first element: ["12", "532"]

Using splice() method

You also can remove elements at any position from an array by using the splice() method. This method removes the element at the specific position and the number of elements you want to remove.

Syntax:

splice(index, number)

Parameters:

  • index: The position to begin you want to remove.
  • number: The number of elements you want to remove.

Return value: The array removed elements.

Example:

// Initialize an array
var arr = ["Learn", "Share", "IT"];

// Get the element at position 1 and remove that element in 'arr' array
arr.splice(1, 1);

// Show array has been deleted element
console.log("New array:", arr);

Output:

New array: ["Learn", "IT"]

Using delete() method

You can remove it at any position with the delete() method.

Syntax:

delete array[pos]

Parameters: 

  • pos: The position needs to be removed.

Return value: A new array without element at the pos position.

Example:

// Initialize an array
var arr = ["Learn", "Share", "IT"];

// Remove element at the position 1
delete arr[1];

// Show array has been deleted element
console.log("New array:", arr);

Output:

New array: ["Learn", null, "IT"]

Using filter() method

This method does not remove elements from our old array. It creates a new array that stores all elements except the value you want to remove from the old array.

Syntax:

filter(bool)

Parameters:

  • bool: A function to execute per element in array, it returns the comparison with the true or false condition.

Return value: A new array removed element(s).

Code example:

// Function check positive numbers
function checkPositiveNumbers(value) {
  	return value > 0;
}

// Initialize array
var arr = [13, 8, 15, -10, 31, 44, -55];

// Function to filter positive numbers in array "arr"
function filterPositiveNumbers() {
  	console.log("New array is:", arr.filter(checkPositiveNumbers));
}

filterPositiveNumbers();

Output:

New array is: [13, 8, 15, 31, 44]

Summary

Those are all methods to remove element(s) from an array in JavaScript. I hope it’s helpful for you. If you have any problems or questions, please comment below. I will answer as possible. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *