Remove data in a vacuum

2

I would like to remove gaps and blank spaces in an array that I get from google sheet and print in this way:

var prueba = ['NOV-DIC 17','ENE-FEB 18','MAR-ABR 18','MAY-JUN 18','JUL-AGO 
18','SEP-OCT 18','NOV-DIC 18','   ','  ','',''];

As you can see there are both empty and blank spaces after that I format it with a join and it looks like this:

NOV-DIC 17,ENE-FEB 18,MAR-ABR 18,MAY-JUN 18,,,, ,

But what I really need is for you to print this:

NOV-DIC 17,ENE-FEB 18,MAR-ABR 18,MAY-JUN 18

without the last commas

    
asked by Rafael Pereira 26.02.2018 в 20:38
source

4 answers

3

Chop the chain after the two commas followed, it would stay like this:

var cadena = 'NOV-DIC 17,ENE-FEB 18,MAR-ABR 18,MAY-JUN 18,,,'
var cadenaCortada = cadena.split(',,')[0]
console.log(cadenaCortada)
    
answered by 26.02.2018 / 20:46
source
2

Possibly the simplest way is to use the method filter of the object Array to catch the elements that are not only composed of empty spaces:

var prueba = ['NOV-DIC 17','ENE-FEB 18','MAR-ABR 18','MAY-JUN 18','JUL-AGO 18','SEP-OCT 18','NOV-DIC 18','   ','  ','',''];

var sinvacios = prueba.filter(e => e.trim() !== '');

console.log(sinvacios.join());
    
answered by 26.02.2018 в 20:51
0

Before printing you can delete them without traversing them or add only those that have content in a new array, such as ...

function limpiarArray(actual) {
  var newArray = new Array();
  for (var i = 0; i < actual.length; i++) {
    if (actual[i]) {
      newArray.push(actual[i]);
    }
  }
  return newArray;
}
let arrLimpio = limpiarArray([1, 2,, 3,, 3,,,,,, 4,, 4,, 5,, 6,,,,]);
console.log(arrLimpio);
    
answered by 26.02.2018 в 20:47
0

With the Array.filter you solve what you want to do

var prueba = ['NOV-DIC 17','ENE-FEB 18','MAR-ABR 18','MAY-JUN 18','JUL-AGO 18','SEP-OCT 18','NOV-DIC 18','   ','  ','','']

prueba = prueba.filter(element => {
  element = element.replace(/ /g,'')
  if (element !== '')
  	return element
})

console.log(prueba)
    
answered by 26.02.2018 в 20:53