Проблемы с выводом сложных массивов / данных объекта с помощью JavaScript
В настоящее время я работаю над страницей, которая собирает динамические данные из представлений веб-приложений в системе управления контентом Business Catalyst. BC позволяет создавать веб-приложения, которые выводят теги в виде {tag_itemid}. Эти теги по сути являются переменными, которые система выводит в форме информации, которую она представляет.
Тем не менее, у меня есть очень сложная страница, которая извлекает данные из нескольких веб-приложений и выводит их в сгенерированные элементы div, которые пользователь может просматривать. У меня проблемы с тем, что страница читает мой массив. Я продолжаю получать сообщение об ошибке "Uncaught SyntaxError: Неожиданный токен [" Ошибка появляется в начале моего массива, allQuestions{tag_itemid}[0] .
Пока игнорируйте теги BC, они не влияют на сценарий. Существуют теги WebApp, которые заполняют пробелы, которые вы можете видеть с переменными, которые я передаю в мой массив.
Если бы вы могли взглянуть на мой код и сказать мне, почему я получаю ошибку, я был бы благодарен, я попытался включить всю соответствующую информацию, не перегружая вас кодом. Также просто отметьте, что все переменные определены на странице или над фрагментами, которыми я поделился с вами - я не получаю никаких неопределенных ошибок.
Спасибо за любую помощь!
var questionAnswers{tag_itemid} = function(qNum, ACount, BCount, CCount, DCount, TCount, FCount, answerType, Rating, SAnswer, qText) {
this.qNum = qNum;
this.ACount = ACount;
this.BCount = BCount;
this.CCount = CCount;
this.DCount = DCount;
this.TCount = TCount;
this.FCount = FCount;
this.Rating = Rating;
this.SAnswer = SAnswer;
this.answerType = answerType;
this.avgAnswer = 0;
this.avgAnswerText = "";
this.formatSAnswers = function() {
var x = this.SAnswer
var y = x.join('"</p></div><div class="sAnswer-data"><p>"');
this.avgAnswer = y;
};
this.qavgAnswer = function() {
if (this.answerType != "" || this.anserType != " ") {
if (this.answerType == "Multiple Choice") {
if (this.ACount > this.BCount && this.ACount > this.CCount && this.ACount > this.DCount) {
this.avgAnswer = this.ACount;
this.avgAnswerText = "A was selected " + this.avgAnswer + " times";
} else if (this.BCount > this.ACount && this.BCount > this.CCount && this.BCount > this.DCount) {
this.avgAnswer = this.BCount;
this.avgAnswerText = "B was selected " + this.avgAnswer + " times";
} else if (this.CCount > this.ACount && this.CCount > this.BCount && this.CCount > this.DCount) {
this.avgAnswer = this.CCount;
this.avgAnswerText = "C was selected " + this.avgAnswer + " times";
} else if (this.DCount > this.ACount && this.DCount > this.BCount && this.DCount > this.CCount) {
this.avgAnswer = this.DCount;
this.avgAnswerText = "D was selected " + this.avgAnswer + " times";
} else if (this.ACount == this.Dcount) {
this.avgAnswer = this.ACount;
this.avgAsnwerText = "Both A and D selected: " + this.avgAnswer + " times.";
} else if (this.ACount == this.Bcount) {
this.avgAnswer = this.ACount;
this.avgAsnwerText = "Both A and B selected: " + this.avgAnswer + " times.";
} else if (this.ACount == this.Ccount) {
this.avgAnswer = this.ACount;
this.avgAsnwerText = "Both A and C selected: " + this.avgAnswer + " times.";
} else if (this.BCount == this.Acount) {
this.avgAnswer = this.BCount;
this.avgAsnwerText = "Both B and D selected: " + this.avgAnswer + " times.";
} else if (this.BCount == this.Ccount) {
this.avgAnswer = this.BCount;
this.avgAsnwerText = "Both B and C selected: " + this.avgAnswer + " times.";
} else if (this.CCount == this.Dcount) {
this.avgAnswer = this.CCount;
this.avgAsnwerText = "Both C and D selected: " + this.avgAnswer + " times.";
} else if (this.ACount == this.BCount && this.ACount == this.CCount) {
this.avgAnswer = this.ACount;
this.avgAnswerText = "Answer A, B, and C were selected: " + this.avgAnswer + " times.";
} else if (this.ACount == this.CCount && this.ACount == this.DCount) {
this.avgAnswer = this.ACount;
this.avgAnswerText = "Answer A, C, and D were selected: " + this.avgAnswer + " times.";
} else if (this.BCount == this.CCount && this.BCount == this.DCount) {
this.avgAnswer = this.BCount;
this.avgAnswerText = "Answer B, C, and D were selected: " + this.avgAnswer + " times.";
} else if (this.ACount == 0 && this.ACount == this.BCount && this.ACount == this.CCount && this.ACount == this.DCount) {
this.avgAnswerText = "There were no selections made for this question.";
} else {
this.avgAnswer = this.ACount;
this.avgAnswerText = "All selections are equal. Answers A, B, C, and D were selected " + this.ACount + " times each.";
};
this.correctAText = "The correct answer:<br/><br/>" + this.avgAnswer;
} else if (this.answerType == "True/False") {
if (this.TCount > this.FCount) {
this.avgAnswer = this.TCount;
this.avgAnswerText = "True was selected " + this.avgAnswer + " times.";
} else if (this.TCount < this.FCount) {
this.avgAnswer = this.FCount;
this.avgAnswerText = "False was selected " + this.avgAnswer + " times.";
} else if (this.TCount == 0 && this.TCount == this.FCount) {
this.avgAnswer = this.TCount;
this.avgAnswerText = "There is not enough data to calculate the average user-selected answer for this question.";
} else {
this.avgAnswer = this.TCount;
this.avgAnswerText = "Both True and False were selected " + this.avgAnswer + " times.";
};
this.correctAText = "The correct answer:<br/><br/>" + this.avgAnswer;
} else if (this.answerType == "Ratings") {
this.correctAText = "This question is evaluated with survey ratings.";
this.Rating = this.Rating / ch{tag_itemid}_userCompletes;
this.avgAnswer = this.Rating;
this.avgAnswerText = "The average rating for this question is " + this.avgAnswer;
} else {
this.correctAText = "This question is evaluated with survey short answers.";
this.formatSanswers();
this.avgAnswerText = 'This question is a short answer survey. Click the button below to see some of the answers.<br/><br/><a class="button small">View responses</a></p></div><div><p class="sAnswer-data">';
};
} else {
this.avgAnswerText = "There is no data for this question. Please try again at a later time.";
};
};
this.insertInfo = function(divID) {
document.getElementById(divName).innerHTML = '<div class="row"><div class="small-12 columns" id="q' + this.qNum + '_text"><p><span class="qNum-data">' + this.qNum + '</span>' + this.qText + '</p></div><div class="row"><div class="small-12 medium-6 columns" id="qAvgAnser' + this.qNum + '"><p class="avgA-data">' + this.avgAnswerText + '</p></div><div class="small-12 medium-6 columns"><p class="avgA-data">' + this.correctAnswer + '</p></div></div></div>';
};
this.showData = function() {
resource = document.createElement('div');
addID = document.createAttribute("id");
divName = "ch{tag_itemid}_ch{tag_chapter number}_q" + this.qNum;
addID.value = divName;
resource.setAttributeNode(addClass);
insertInfo(divName);
};
this.generateInfo(passedDivID) = function() {
passedDivID = passedDivID + this.showData();
};
};
Обратите внимание, что здесь есть фактический массив, я ограничил его четырьмя элементами, в отличие от 15, которые он на самом деле содержит.
var allQuestions{tag_itemid} = [];
var allQuestions{tag_itemid}[0] = questionAnswers{tag_itemid}("1", ch{tag_itemid}_ch{tag_chapter number}QAavg1, ch{tag_itemid}_ch{tag_chapter number}QBavg1, ch{tag_itemid}_ch{tag_chapter number}QCavg1, ch{tag_itemid}_ch{tag_chapter number}QDavg1, ch{tag_itemid}_ch{tag_chapter number}QTavg1, ch{tag_itemid}_ch{tag_chapter number}QFavg1, ch{tag_itemid}_ch{tag_chapter number}qAType1, ch{tag_itemid}_ch{tag_chapter number}Rate1Q, ch{tag_itemid}_ch{tag_chapter number}SA1Q, ch{tag_itemid}_ch{tag_chapter number}QText1);
var allQuestions{tag_itemid}[1] = questionAnswers{tag_itemid}("2", ch{tag_itemid}_ch{tag_chapter number}QAavg2, ch{tag_itemid}_ch{tag_chapter number}QBavg2, ch{tag_itemid}_ch{tag_chapter number}QCavg2, ch{tag_itemid}_ch{tag_chapter number}QDavg2, ch{tag_itemid}_ch{tag_chapter number}QTavg2, ch{tag_itemid}_ch{tag_chapter number}QFavg2, ch{tag_itemid}_ch{tag_chapter number}qAType2, ch{tag_itemid}_ch{tag_chapter number}Rate2Q, ch{tag_itemid}_ch{tag_chapter number}SA2Q, ch{tag_itemid}_ch{tag_chapter number}QText2);
var allQuestions{tag_itemid}[2] = questionAnswers{tag_itemid}("3", ch{tag_itemid}_ch{tag_chapter number}QAavg3, ch{tag_itemid}_ch{tag_chapter number}QBavg3, ch{tag_itemid}_ch{tag_chapter number}QCavg3, ch{tag_itemid}_ch{tag_chapter number}QDavg3, ch{tag_itemid}_ch{tag_chapter number}QTavg3, ch{tag_itemid}_ch{tag_chapter number}QFavg3, ch{tag_itemid}_ch{tag_chapter number}qAType3, ch{tag_itemid}_ch{tag_chapter number}Rate3Q, ch{tag_itemid}_ch{tag_chapter number}SA3Q, ch{tag_itemid}_ch{tag_chapter number}QText3);
var allQuestions{tag_itemid}[3] = questionAnswers{tag_itemid}("4", ch{tag_itemid}_ch{tag_chapter number}QAavg4, ch{tag_itemid}_ch{tag_chapter number}QBavg4, ch{tag_itemid}_ch{tag_chapter number}QCavg4, ch{tag_itemid}_ch{tag_chapter number}QDavg4, ch{tag_itemid}_ch{tag_chapter number}QTavg4, ch{tag_itemid}_ch{tag_chapter number}QFavg4, ch{tag_itemid}_ch{tag_chapter number}qAType4, ch{tag_itemid}_ch{tag_chapter number}Rate4Q, ch{tag_itemid}_ch{tag_chapter number}SA4Q, ch{tag_itemid}_ch{tag_chapter number}QText4);
А вот скрипт, взаимодействующий с массивом:
for (var i = 0; i <= 14; i++) {
if (allQuestions[i].qText != "" || allQuestions[i].qText != " ") {
allQuestions[i].qavgAnswer();
allQuestions[i].generateInfo(quizDiv{tag_itemid});
};