jquery数组导致div

时间:2013-03-20 05:31:33

标签: jquery arrays

我想在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 个答案:

答案 0 :(得分:1)

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

试试这个

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

答案 1 :(得分: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());

演示:Fiddle

答案 2 :(得分: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

答案 3 :(得分:0)

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

}