
- 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
Subtracting array in JavaScript Delete all those elements from the first array that are also included in the second array
Suppose, we have two arrays of literals like this −
const arr1 = ['uno', 'dos', 'tres', 'cuatro']; const arr2 = ['dos', 'cuatro'];
We are required to write a JavaScript function that takes in two such arrays and delete all those elements from the first array that are also included in the second array.
Therefore, for these arrays, the output should look like this −
const output = ['uno', 'tres'];
Example
const arr1 = ['uno', 'dos', 'tres', 'cuatro']; const arr2 = ['dos', 'cuatro']; const findSubtraction = (arr1 = [], arr2 = []) => { let filtered = []; filtered = arr1.filter(el => { if(arr2.indexOf(el) === -1){ return true; }; }); return filtered; }; console.log(findSubtraction(arr1, arr2));
Output
And the output in the console will be −
[ 'uno', 'tres' ]
- Related Articles
- Sort the second array according to the elements of the first array in JavaScript
- Return the bases when first array elements are raised to powers from second array in Python
- Set the first array elements raised to powers from second array element-wise in Numpy
- Difference between first and the second array in JavaScript
- Maximize first array over second in JavaScript
- Delete all elements in an array field in MongoDB?
- JavaScript Checking if all the elements are same in an array
- Find the first, second and third minimum elements in an array in C++
- First string from the given array whose reverse is also present in the same array in C++
- Find elements which are present in first array and not in second in C++
- Return a subarray that contains all the element from the original array that are larger than all the elements on their right in JavaScript
- How to filter an array from all elements of another array – JavaScript?
- Return an array of all the indices of minimum elements in the array in JavaScript
- Delete elements in first string which are not in second string in JavaScript
- How to delete elements from an array?

Advertisements