【发布时间】:2017-11-23 05:16:07
【问题描述】:
function getQuestionHtml(question) {
var result = [];
var result2 =[];
if(question.responses.length > 1) {
result.push('<div class="question-answered">');
result.push('<span>' + question.question_text + '</span>');
console.log(result);
}
else{
result2.push('<div class="question-answered">');
result2.push('<span>' + question.question_text + '</span>');
result2.push('<div style="font-size: 12px;">No predictions found for this question.</div>')
console.log(result2)
}
Array.prototype.push.apply(result,result2);
return result.join("");
}
我想在数组result 的末尾附加result2,但由于if 和else 条件,目前我不想要它的附加替代。
有人可以帮我在result 末尾附加result2。
【问题讨论】:
-
如果你想在这种情况下追加:
question.responses.length < 1,如果不是这样:if(question.responses.length >= 1) {? -
这没有意义。如果您正在执行
else代码,则result数组中没有任何内容可附加。 -
你为什么不直接在两个分支中推入
result。
标签: javascript jquery html arrays