2
$.ajax({
           type: 'POST',
           url: 'url',
           data: val,
           async: false,
           dataType: 'json',
           success: function (max) {
               console.log(max.origin);
           }
       });

Then i am getting an output which is

["test,origin"]

I want to split it like .

 test
 origin

Please suggest me .

Much appreciated

3
  • 2
    Are you getting like ["test,origin"] or ["test","origin"] ? Commented Feb 12, 2015 at 19:01
  • If you could add your max json object structure to your question that would be helpful. Commented Feb 12, 2015 at 19:10
  • So is this a whole string like '["test,origin"]'? Commented Feb 12, 2015 at 19:31

2 Answers 2

1

If your returned value of max.origin really is ["test,origin"], you have to do something like this

var data = max.origin[0].split(',');

so that data will contain a list of the elements that are comma-separated. However, if your max.origin is in the form of ["test", "origin"], you can simply select each/all items by going through a for-loop and (optionally) print them out:

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

If you know that you only get the two elements (test and origin) each time, @void's answer is a good approach.

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

2 Comments

No what he is getting is a string not an array
Yes, but the string is within an array of length 1.
1

max.origin[0].split(",")[0] will give you test

max.origin[0].split(",")[1] will give you origin

1 Comment

Doesn't look like they are.. like Sai sudhakar's comment says, it depends if the output is ["test,origin"] or ["test","origin"]

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.