0

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!

2
  • Which variable hold the value my name is,how are you,i am good,thank you,hey you,? Commented Jan 22, 2016 at 2:52
  • data.content holds the phrase. =) Commented Jan 22, 2016 at 2:53

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

no, the data.number simply holds how many values are there. (It holds something like "1,3,2,5,4". data.content holds the phrases
Then you should split the content itself, see updated answer
Thank you. Should have realized to split the content as well. Thank you. Got it working! =)
Just a quick question. Because a phrase can often contain "," as a part of the sentence, this might screw up splits functions. I was thinking maybe I will add unusual symbols like ",*&^" instead of "," so there is an unique distinction between phrases. What do you think?
Then you need to look more about split function using regular expression to match if you have more than one symbols like to split with any kind of separation. Like his guest271314 answer. As example data.split(/,|&/)
|
1

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/

Comments

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>

Comments

1

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);
    });                   
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.