Salman Razak Memon
Salman Razak Memon

Reputation: 93

jquery array result in a div

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

Answers (4)

tamilmani
tamilmani

Reputation: 591

var myArray = [{Title : "one" }, {Title: "two"}, {Title: "three"}];
for(var i in myArray)
{
var title = myArray[i];
alert(title.Title);

}

Upvotes: 0

bipen
bipen

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

Arun P Johny
Arun P Johny

Reputation: 388316

it should be

var myArray = [{Title : "one" }, {Title: "two"}, {Title: "three"}];
$(myArray).each(function() {
    $(".myarray").append(this.Title);
});

Demo: Fiddle or Fiddle2

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

pdoherty926
pdoherty926

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);

Fiddle

Upvotes: 1

Related Questions