• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Remove All Instances of Value From an Array in JavaScript

Remove All Instances of Value From an Array in JavaScript

June 6, 2022 Leave a Comment

To remove all instances of a value from an array in JavaScript, the easiest way is to use the JavaScript array filter() function.

Below shows an example of how you could remove the number ‘1’ from an array of numbers with filter().

var array_of_numbers = [1,2,3,4,1,2,1,4,3,2];

var filtered_array = array_of_numbers.filter(x => x != 1);

console.log(filtered_array);

// Output:
[2, 3, 4, 2, 4, 3, 2]

If you want to be more explicit with the callback function to filter(), you can do the following.

var array_of_numbers = [1,2,3,4,1,2,1,4,3,2];

var filtered_array = array_of_numbers.filter(function(x) {
  return x != 1;
});

console.log(filtered_array);

// Output:
[2, 3, 4, 2, 4, 3, 2]

When working with arrays in JavaScript, it can be valuable to be able to easily filter and remove unwanted values from your array.

One such situation is where you may want to remove all instances of a certain value from an array.

We can easily remove all instances from an array of a value in JavaScript using the JavaScript array filter() function.

To use filter(), you need to pass a callback function which will provide logic to identify the value or values you want to remove.

Below are a few more examples of how you can use filter() to filter out values from arrays in JavaScript.

If you have an array of strings, you can easily filter out any empty strings with filter().

var array_of_strings = ["This","","is","an","array","","with","empty","","strings","."];

var filtered_array = array_of_strings.filter(x => x != "");

console.log(filtered_array);

// Output:
['This', 'is', 'an', 'array', 'with', 'empty', 'strings', '.']

Using filter() to Remove and Filter Out Unwanted Values in JavaScript Array

The JavaScript filter() function is extremely useful and powerful when working with arrays in JavaScript.

If needed, you can pass complex conditional statements in the callback function for filter().

var array_of_strings = ["This","","is","an","array","","with","empty","","strings","."];

var filtered_array = array_of_strings.filter(x => !["This",""].includes(x));

console.log(filtered_array);

// Output:
['is', 'an', 'array', 'with', 'empty', 'strings', '.']

filter() also works on arrays of JavaScript objects and you can filter based on the names of the object.

var array_of_js_objects = [{"name":"Jim","age":30},{"name":"Sally","age":20},{"name":"Paul","age":25},{"name":"Suzie","age":35}];

var filtered_array = array_of_js_objects.filter(x => x["age"] > 25 && x["name"][0] == "J");

console.log(filtered_array);

// Output:
[{name: 'Jim', age: 30}]

Hopefully this article has been useful for you to learn how to remove all instances from an array in JavaScript.

Other Articles You'll Also Like:

  • 1.  Remove Commas From Array in JavaScript
  • 2.  Using JavaScript to Format the Date in mm dd yyyy
  • 3.  Using JavaScript to Remove All Pipe Characters From String
  • 4.  JavaScript Sorted Map – How to Sort a Map by Keys or Values
  • 5.  Using JavaScript to Check if Variable is a String
  • 6.  Add Commas to Number in JavaScript
  • 7.  Reverse Words in a String JavaScript
  • 8.  JavaScript cosh – Find Hyperbolic Cosine of Number Using Math.cosh()
  • 9.  Using JavaScript to Replace a Character at an Index
  • 10.  Remove Undefined Values From an Array in JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy