John John - 3 months ago 8
Javascript Question

printing all records using only one alert

I have the following code with the JSFiddle for the same here:

var result = [
{"Id": 10004, "PageName": "club"},
{"Id": 10040, "PageName": "qaz"},
{"Id": 10059, "PageName": "jjjjjjj"}
];

$.each(result, function(i, item) {
alert(result[i].PageName);
});


In order to see all the results, I have to click Ok in alert window two times. How can I display the contents using one alert only?

Answer

You can use map() and join() and return string with page names.

var result = [ 
 {"Id": 10004, "PageName": "club"}, 
 {"Id": 10040, "PageName": "qaz"}, 
 {"Id": 10059, "PageName": "jjjjjjj"}
];

var r = result.map(function(e) {
  return e.PageName;
}).join(' ');

alert(r);