0

I have an input in which I can type tags separated by comma. Said input can receive a string in this way:

hola, hello, hola hello, , hola,

The string is then sent to the backend of my app in this way:

req.body.tags.split(',').map((tag) => tag.trim())

This only helps to split the string by commas

string1, string2

That's great but what if I had a string as the one shown below:

string1, string2, , string4

How can I ignore or remove the empty 'object' between the commas after string2 and the comma after string4?. Not only that, what if I had a string that looks similar to the first one I mentioned before?

string1, string2, , string4, string5,

Let's not forget about the spaces(which I would like to replace with a hyphen):

string1, string2, , string4, string 5, string6,

How would I ignore the empty object after the last comma also?

I'm asking because this is how it usually looks on my mongoDB after the data is sent:

tags: [
  "string1",
  "string2",
  "",
  "string4",
  "string5",
  ""
]

I'm just trying to avoid having empty objects.

UPDATE ONE:

req.body.tags.split(',').map((tag) => tag.trim()).filter((tag) => tag.length !== 0)

It works great. With that being said, I still need to replace the spaces with hyphens. I tried this and did not work:

req.body.tags
          .split(',')
          .map((tag) =>
            tag
              .trim()
              .replace(/[\s]+/g, '-')
              .toLowerCase()
          )
          .filter((tag) => tag.length !== 0)
Kirasiris
  • 523
  • 10
  • 37

2 Answers2

2

You can use filter function to remove empty strings from array.

const tags = 'hola, hello, hola hello, , hola,';
const res = tags
  .split(',')
  .map((tag) => tag.trim())
  .filter((tag) => tag.length !== 0);
console.log(res);

If you want to replace all spaces from the string item of the array, you can do it as below.

const tags = 'h o la, hello, hola hello, , hola,';
const res = tags
  .split(',')
  .map((tag) => tag.trim().replace(/[\s]+/g, '-').toLowerCase())
  .filter((tag) => tag.length !== 0);
console.log(res);
michael
  • 4,053
  • 2
  • 12
  • 31
  • Thank you sir for this!. If you don't mind, do you have any idea on how replace the spaces found within a single object from the array? I tried this but did not work: `req.body.tags .split(',') .map((tag) => tag .trim() .replace(/[\s]+/g, '-') .toLowerCase() ) .filter((tag) => tag.length !== 0)` – Kirasiris Dec 04 '20 at 01:49
  • @Kirasiris Is this result you want? `'ho la, hello, hola hello, , hola,'` => `[ 'ho-la', 'hello', 'hola-hello', 'hola' ]` – michael Dec 04 '20 at 01:56
  • 1
    I think your code (in the comment) works fine. let me try and update it on my answer – michael Dec 04 '20 at 01:58
  • It did not work on my end first, then I had to restart the server and it suddenly worked. – Kirasiris Dec 04 '20 at 02:05
0

You could filter the array for empty characters to prevent this.

var sparseArray = [0, , , 1, , , , , 2, , , , 3],
cleanArray = sparseArray.filter(function () { return true });
console.log(cleanArray); // [ 0, 1, 2, 3 ]

see full post here

DeadApe
  • 416
  • 1
  • 3
  • 10