0
var test = ["", "A Sample PDF.pdf", " ", "file_example_JPG_100kB.jpg", " "]

expected o/p : ["A Sample PDF.pdf","file_example_JPG_100kB.jpg"]

I have tried as :

  1. split by '"' (delimiter as ")

  2. replace spaced strings to empty and then arr.filter(Boolean)

Hope there would an easy understandable way to do this , any help is appreciated

KcH
  • 3,302
  • 3
  • 21
  • 46

2 Answers2

1

try like this.

var test = ["", "A Sample PDF.pdf", " ", "file_example_JPG_100kB.jpg", " "];

var newT = [];

for(var ind = 0; ind < test.length; ind++){
    if((test[ind].trim()).length > 0){
        newT.push(test[ind]);
    }
}

console.log(newT);
Nbody
  • 1,168
  • 7
  • 33
1

You can try something like this:

var test = ["", "A Sample PDF.pdf", " ", "file_example_JPG_100kB.jpg", " "]

var filteredData = test.filter(i => {
  return i.replace(/ /g, "").length > 0
})

console.log(filteredData);
Prerak Sola
  • 9,517
  • 7
  • 36
  • 67