2

On a AjAX call , i am getting data

success : function(data){
               alert(data);
         }

This is the result of that alert

[/files/1.jpg,/files/2.jpg,/files/3.jpg]

I want to push this result into a javascript array

 var outputdata = [];

Where the array outputdata should look like

 var outputdata = [/files/1.jpg,/files/2.jpg,/files/3.jpg];

I tried this way

success : function(data){
               alert(data);
outputdata.push(data);

         }

Updated Part

I tried with yours as wel as with this

success: function(data) {
                $.each(data, function(i) {

                    outputdata.push(data[i]);
                });
                alert(outputdata);

                outputdata.forEach(function (element) {

                      content = "<div><a href='#'><img src=" + element + "   /></a></div>";
                     $("#container").append(content);
                    });

            }

enter image description here

The initial alert data is fine (I mean initial means )

success: function(data) {

             alert(data);

                $.each(data, function(i) {

                    outputdata.push(data[i]);
                });
                alert(outputdata);

See the Picture of initial alert

enter image description here

2
  • 2
    Super-classy taskbar you've got there. Commented Apr 21, 2012 at 20:10
  • 2
    You might want to be sure and clear your browser history before your mom gets home. Commented Aug 1, 2015 at 7:25

1 Answer 1

3

Why would you do that? data is already an array and you can call the elements by

data[i]

Or is it a data string that is returned from you ajax call? Than you need to parse it first, like

var your_data = JSON.parse(data);

and can call it by

your_data[i]

Anyway, looping an array would look like this:

success: function(data) {
    $.each(data, function(i) {
        outputdata[i] = data[i];
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks , i tried either way but not working , please see the updated question
I am more then confused by your screenshot now.

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.