使用值对从Javascript数组附加文本。想要打印出来,只显示[对象,对象]

时间:2015-01-12 02:12:21

标签: javascript jquery html

使用值对从Javascript数组附加文本。想要打印出来,只显示[对象,对象] 见以下链接: http://jsfiddle.net/akpn3/524/

var myFlashcards = [
{question:"Am I 24?",answer:"Yes"},{question:"Am I 6?",answer:"No"},{question:"When $a \\ne 0$, there are two solutions to \\(ax^2 + bx + c = 0\\) and they are:",answer:"$$x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}.$$"}
];
$('.listprice').append(myFlashcards.toString());

3 个答案:

答案 0 :(得分:0)

这是你想要的吗?

尝试使用JSON.stringify转换json object 以显示

http://jsfiddle.net/akpn3/525/

答案 1 :(得分:0)

这是因为toString()object调用将返回[Object object]。尝试使用类似的东西:

$.each(myFlashcards, function(i,n){
  $('.listprice').append('<div>'+n.question+'</div><div>'+n.answer+'</div>');
});

如果你想打印类似JSON的字符串,请使用stringify。

$('.listprice').append('<pre>'+JSON.stringify(myFlashCard)+'</pre>');

答案 2 :(得分:0)

如果你必须读取数组中的所有对象,那么应该这样做:

for (var element in myFlashcards)
{
    $('.listprice').append( '<p>' + element.question + ' ' + element.answer + '</p><br />' );
}