Replace values of a string [closed]

-1

I have a variable

var fruits = "Banana, Orange, Apple, Mango";

What I need is that every one that finds a "," skip the line with a "\ n" and good disappears the "," so it looks like this:

Banana

Orange

Apple

Mango
    
asked by Diego Alonso 20.03.2018 в 16:56
source

1 answer

1

We replace the spaces in the string and then we separate it by the commas

var array_fruits = fruits.replace(/ /g, '').split(',')

Then we iterate the array in which we have converted the string

for(var i = 0; i < array_fruits.length; i++){ 
   console.log(array_fruits[i]) 
}

Result:

Banana
Orange
Apple
Mango
    
answered by 20.03.2018 / 17:08
source