Reputation: 32321
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);
});
}
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
Upvotes: 2
Views: 27297
Reputation: 2578
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];
});
}
Upvotes: 3