Reputation: 93
i am trying to show array result in a div. please help
var events = [
{ Title: "Five K for charity", Date: new Date("03/13/2013"), Time: "11:15" },
{ Title: "Dinner", Date: new Date("03/25/2013"), Time: "11:15" },
{ Title: "Meeting with manager", Date: new Date("03/01/2013"), Time: "11:15" }
];
var myArray = [{Title : "one" }, {Title: "two"}, {Title: "three"}];
$(myArray).each(function() {
$(".myarray").text();
});
please advice where I am wrong?
Upvotes: 0
Views: 41
Reputation: 591
var myArray = [{Title : "one" }, {Title: "two"}, {Title: "three"}];
for(var i in myArray)
{
var title = myArray[i];
alert(title.Title);
}
Upvotes: 0
Reputation: 36531
since your myArray is array of object .. so use loop to get array object and .
operator to get that particular objects value...
try this
var myArray = [{Title : "one" }, {Title: "two"}, {Title: "three"}];
$.each(myArray,function(i,v) {
$(".myarray").append(v.Title);
});
Upvotes: 1
Reputation: 388316
it should be
var myArray = [{Title : "one" }, {Title: "two"}, {Title: "three"}];
$(myArray).each(function() {
$(".myarray").append(this.Title);
});
Or Better
var myArray = [{Title : "one" }, {Title: "two"}, {Title: "three"}];
$(".myarray").append($.map(myArray, function(v, i){
return v.Title;
}).join());
Demo: Fiddle
Upvotes: 1
Reputation: 10349
var myArray = [{Title : "one" }, {Title: "two"}, {Title: "three"}];
var text = myArray.map(function(o) {
return '<p>' + o.Title + '</p>';
}).join(' ');
$(".myarray").html(text);
Upvotes: 1