0

我正在尝试在 div 中显示数组结果。请帮忙

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

请指教我哪里错了?

4

4 回答 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);

小提琴

于 2013-03-20T05:38:52.823 回答
1

因为你的 myArray 是对象数组..所以使用循环来获取数组对象和.运算符来获取特定对象的值......

尝试这个

 var myArray = [{Title : "one" }, {Title: "two"}, {Title: "three"}];
 $.each(myArray,function(i,v) {
   $(".myarray").append(v.Title);
 });
于 2013-03-20T05:33:26.327 回答
1

它应该是

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

演示:FiddleFiddle2

或更好

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

$(".myarray").append($.map(myArray, function(v, i){
    return v.Title;
}).join());

演示:小提琴

于 2013-03-20T05:33:52.240 回答
0
var myArray = [{Title : "one" }, {Title: "two"}, {Title: "three"}];
for(var i in myArray)
{
var title = myArray[i];
alert(title.Title);

}
于 2013-03-20T06:00:28.157 回答