0

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?

4 Answers 4

1
var myArray = [{Title : "one" }, {Title: "two"}, {Title: "three"}];

var text = myArray.map(function(o) {
    return '<p>' + o.Title + '</p>';
}).join(' ');

$(".myarray").html(text);

Fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

1 Comment

Won't this result in <div class='myarray'>three</div>?
0
var myArray = [{Title : "one" }, {Title: "two"}, {Title: "three"}];
for(var i in myArray)
{
var title = myArray[i];
alert(title.Title);

}

Comments

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.