Steve Kim
Steve Kim

Reputation: 5591

jQuery separating string of phrase values

So, I have the following js:

function(data){
    var length  = (data.number.split(',').length) - 1; //Holds numbers such as "1,2,3,4,5" which gives "5".         
    var content = data.content; //Holds the phrases
    alert(content); 
        for (var i=0; i<length; i++) {
                var indi_cont = data.content[i];                    
                alert(indi_cont);                   
            };                      
        };  

Here, the length is 5 and I get 5 array of values for the alert(content) (note the " , " at the end of values).

`my name is,how are you,i am good,thank you,hey you,`

Using the for function, I want to separate each value as below:

my name is
how are you
i am good
thank you
hey you

However, i am getting alert(indi_cont) as following:

m
y
n
a
m

So, it looks like individual characters are shown instead of the whole phrase.

How do fix this?

Thanks!

Upvotes: 0

Views: 40

Answers (4)

Pedro Estrada
Pedro Estrada

Reputation: 2404

This is how i would do it:

function(data){
    var values = data.content.split(',');
    $.each(values, function (i, value) {
        if (value.length > 0)
            console.log(value);
    });                   
};

Upvotes: 1

guest271314
guest271314

Reputation: 1

Try using RegExp /,|\s/ at .split() to split input text at comma character "," and " " space character, Array.prototype.filter() with parameter Boolean to remove empty " " from results of .split()

var data = "my name is,how are you,i am good,thank you,hey you,";

var content = data.split(/,|\s/).filter(Boolean);

console.log(content)

for (var i = 0; i < content.length; i++) {
  document.body.innerHTML += content[i] + "<br>"
}
<body>
  
</body>

Alternatively, using .match() with RegExp /[a-z0-9]+[^,\s]/ig to match alphanumeric characters negating comma characters "," or space " " characters

var data = "my name is,how are you,i am good,thank you,hey you,";

var content = data.match(/[a-z0-9]+[^,\s]/ig);

console.log(content)

for (var i = 0; i < content.length; i++) {
  document.body.innerHTML += content[i] + "<br>"
}
<body>
  
</body>

Upvotes: 1

Norlihazmey Ghazali
Norlihazmey Ghazali

Reputation: 9060

You should store the returned string from split like following :

function(data){
   //var length  = (data.number.split(',').length) - 1; //gives "5" 
   var splitStr = data.content.split(',');         
   //var content = data.content; 
   //alert(content); 
   for (var i=0; i< (splitStr.length - 1 ); i++) {
       var indi_cont = splitStr[i];                    
       alert(indi_cont);                   
   }                     
} 

DEMO

Upvotes: 1

Chris Jaquez
Chris Jaquez

Reputation: 669

You need to store the return value of the split function in array, not the length of it.

Try:

function(data){
    var dataArray = data.split(',');
    for (var i=0; i<dataArray.length; i++) {
        var indi_cont = dataArray[i];                    
        console.log(indi_cont);                   
    };                      
};

fiddle: https://jsfiddle.net/bz9h95ox/

Upvotes: 1

Related Questions