
- 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
Inserting empty string in place of repeating values in JavaScript
We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end.
For example: If we find 4 duplicate values we have to remove then all and insert four empty strings at the end.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const arr = [1,2,3,1,2,3,2,2,3,4,5,5,12,1,23,4,1]; const deleteAndInsert = arr => { const creds = arr.reduce((acc, val, ind, array) => { let { count, res } = acc; if(array.lastIndexOf(val) === ind){ res.push(val); }else{ count++; }; return {res, count}; }, { count: 0, res: [] }); const { res, count } = creds; return res.concat(" ".repeat(count).split(" ")); }; console.log(deleteAndInsert(arr));
Output
The output in the console will be −
[ 2, 3, 5, 12, 23, 4, 1, '', '', '', '', '', '', '', '', '', '', '' ]
- Related Articles
- How to create Empty Values String in JavaScript?
- Removing duplicates and inserting empty strings in JavaScript
- Repeating letter string - JavaScript
- Return index of first repeating character in a string - JavaScript
- Inserting string at position x of another string using Javascript
- Detecting the first non-repeating string in Array in JavaScript
- Check if a string is repeating in itself in JavaScript
- Finding the first non-repeating character of a string in JavaScript
- Finding the index of the first repeating character in a string in JavaScript
- Repeating string for specific number of times using JavaScript
- Sorting string alphabetically and inserting underscores using JavaScript
- Inserting rows in an empty table to test output of ABAP code
- From a list of IDs with empty and non-empty values, retrieve specific ID records in JavaScript
- How do you reverse a string in place in JavaScript?
- Repeating each character number of times their one based index in a string using JavaScript

Advertisements