
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Removing Negatives from Array in JavaScript
The given task is to remove the negative values from the array.
Input-Output Scenario
Let’s look into input output scenarios. Consider there is an array and it is having both negative and positive integer values. We need to remove the negative values from the array and return the array.
Input = [-2, 5, -7, 32, 78, -32]; Output = [5, 32, 78]
Now let’s assume there are no negatives integer values in the array. so, the array will be returned in the same order.
Input = [56, 43, 12, 67, 69, 34]; Output = [56, 43, 12, 67, 69, 34]
Using the pop() method
The pop() method in JavaScript will remove the last element in the array and it changes or modifies the original array.
Syntax
Following is the syntax of pop() method, There will be no parameters for this method.
array.pop()
Example
In the following example, we have sorted in the descending order by using compare function. So that the negative numbers will be at last positions in the array. Then by using pop() method we have removed the last indices until the there are no more negative integers. Thus the array will have positive integers.
<!DOCTYPE html> <html> <head> <p id = "para"></p> </head> <body> <script> const array = [45, 23, 12, -4, 76, 56, 43, -9]; array.sort(function(a,b) { return b - a; }); let x = array.length for (x; x--;) { if ( array[x] < 0 ){ array.pop(); } } document.getElementById("para").innerHTML = "Array after removing negatives values from it:" + array; </script> </body> </html>
If we observe the following output, it returned the array by deleting the negative elements in the array in descending order −
Using the splice() method
The splice() method in JavaScript will add or remove the elements in an array and this method will change the original array.
Syntax
Below is the syntax of splice() method,
array.splice(index, numofelements, item1, ....., itemN)
Where,
The first parameter(index) is for the position to add or delete the elements. And it is a mandatory parameter.
Second parameter is for number of elements to be removed. This is an optional parameter
Example
In the example, we have declared an array with both negative and positive integers. Then we have iterated through the array elements by for loop and if there is any element which is less than 0, the splice method will remove that particular element.
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <button onClick = "func()"> Click to remove (-)'s </button> <p id = "para"></p> <script> function func() { const arr = [09, 38, 45, -25, 42, -12, 98]; let x = 0; for (x; x < arr.length; x++){ if (arr[x] < 0) arr.splice(x, 1); } document.getElementById("para").innerHTML = arr; } </script> </body> </html>
In the output, we can see that the negative elements have been removed with splice method and returned the array.
- Related Articles
- JavaScript Algorithm - Removing Negatives from the Array
- Removing duplicate objects from array in JavaScript
- Removing an element from an Array in Javascript
- Removing comments from array of string in JavaScript
- Removing redundant elements from array altogether - JavaScript
- Completely removing duplicate items from an array in JavaScript
- Removing all the empty indices from array in JavaScript
- Removing duplicates from a sorted array of literals in JavaScript
- Removing consecutive duplicates from strings in an array using JavaScript
- Removing an element from the end of the array in Javascript
- Removing an element from the start of the array in javascript
- Removing item from array in MongoDB?
- Removing an element from a given position of the array in Javascript
- Removing identical entries from an array keeping its length same - JavaScript
- Removing the odd occurrence of any number/element from an array in JavaScript
