
- 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
Delete elements in first string which are not in second string in JavaScript
We are required to write a JavaScript function that takes in two strings. Our function should return a newer version of the first string that contains only those elements that are present in the second string as well.
Note that the order of appearance of elements in the returned sting should not change, i.e., the order should be the same as it was in the first string.
Example
The code for this will be −
const str1 = 'abcdefgh'; const str2 = 'banana'; const deleteSelectively = (str1 = '', str2 = '') => { let strArr1 = str1.split(''); const strArr2 = str2.split(''); const map = {}; strArr2.forEach(el => { map[el] = 1; }); strArr1 = strArr1.filter(el => { return map.hasOwnProperty(el); }); return strArr1.join(''); }; console.log(deleteSelectively(str1, str2));
Output
And the output in the console will be −
ab
- Related Articles
- Python Program that Displays which Letters are in the First String but not in the Second
- Find elements which are present in first array and not in second in C++
- Is the second string a rotated version of the first string JavaScript
- Subtracting array in JavaScript Delete all those elements from the first array that are also included in the second array
- Remove all characters of first string from second JavaScript
- Dynamic Programming: Is second string subsequence of first JavaScript
- Return TRUE if the first string starts with a specific second string JavaScript
- Count elements present in first array but not in second in C++
- Check whether second string can be formed from characters of first string in Python
- Elements present in first array and not in second using STL in C++
- Second most frequent character in a string - JavaScript
- Finding second smallest word in a string - JavaScript
- Find the character in first string that is present at minimum index in second string in Python
- How to concatenate two strings so that the second string must concatenate at the end of first string in JavaScript?
- How to concatenate two strings so that the second string must concatenate at the end of the first string in JavaScript?

Advertisements