I have an array of strings in javaScript:
array = ['xx', 'xxxxxxxx', 'xxx'];
I want to reach this:
array = ['', '', '']; //empty the strings but keep the length of array
What is the best way?
Use Array.fill.
array = ['xx', 'xxxxxxxx', 'xxx'];
array.fill('');
console.log(array)
If you need to create a new array, use the Array constructor combined with Array.fill:
array = ['xx', 'xxxxxxxx', 'xxx'];
const newArray = new Array(array.length);
newArray.fill('');
console.log(newArray)
array = array.map(function(item){return '';});
array.map(() => '')simple