QScript Functions for Processing Arrays
This page contains functions for working with arrays of objects specific to Q, like questions, variables, and tables.
To make these functions available when writing a QScript or Rule see JavaScript Reference.
getNamesOfQuestionsInArray(question_array)
Given an array of questions, this function returns an array of the corresponding question names.
getNamesOfQuestionsAndDataFilesInArray(question_array)
Given an array of questions, this function returns an array of the corresponding question and data file names.
getNamesOfVariablesInArray(variable_array)
Given an array of variables, this function returns an array containing the corresponding variable labels.
getLabelsOfVariablesInArray(variable_array)
Given an array of variables, this function returns an array containing the corresponding variable names.
getNamesAndLabelsOfVariablesInArray(variable_array)
Given an array of variables, this function returns an array containing the corresponding variable names and labels.
getQuestionNamesAndLabelsOfVariablesInArray(variable_array)
Given an array of variables, this function returns an array containing the corresponding question names and variable labels.
getQuestionNamesAndLabelsAndDataFilesOfVariablesInArray(variable_array)
Given an array of variables, this function returns an array containing the corresponding question names and variable labels and data file names.
getNamesOfDataFilesInArray(datafile_array)
Given an array of data files, this function returns an array of the corresponding data file names.
getNamesOfTablesInArray(table_array)
Given an array of tables, this function returns an array containing the corresponding table names.
removeTextVariablesFromArray(variable_array)
This function removes any Text variables from the input variable_array. This is useful when getVariablesByName produces an array of variables for a question that also includes an Other/Specify variable which cannot be set into the same question as the rest of the variables in the array.
Source Code
includeWeb("QScript Utility Functions");
// Given an array of questions, return an array of their names
function getNamesOfQuestionsInArray(question_array) {
checkQObjectArrayType(question_array, "Question");
return question_array.map(q => q.name);
}
function getNamesOfQuestionsAndDataFilesInArray(question_array) {
checkQObjectArrayType(question_array, "Question");
return question_array.map(q => q.name + ' [' + q.dataFile.name + ']');
}
// Given an array of variables, return an array of their names
function getNamesOfVariablesInArray(variable_array) {
checkQObjectArrayType(variable_array, "Variable");
return variable_array.map(v => v.name);
}
// Given an array of variables, return an array of their labels
function getLabelsOfVariablesInArray(variable_array) {
checkQObjectArrayType(variable_array, "Variable");
return variable_array.map(v => v.label);
}
// Given an array of variables, return an array of their names and labels
function getNamesAndLabelsOfVariablesInArray(variable_array) {
checkQObjectArrayType(variable_array, "Variable");
return variable_array.map(v => v.name + ": " + v.label);
}
function getQuestionNamesAndLabelsOfVariablesInArray(variable_array) {
checkQObjectArrayType(variable_array, "Variable");
return variable_array.map(v => getQuestionNameAndVariableLabel(v));
}
function getQuestionNamesAndLabelsAndDataFilesOfVariablesInArray(variable_array) {
checkQObjectArrayType(variable_array, "Variable");
return variable_array.map(v => getQuestionNameAndVariableLabel(v) + ' [' + v.question.dataFile.name + ']');
}
function getNamesOfDataFilesInArray(datafile_array) {
checkQObjectArrayType(datafile_array, "DataFile");
return datafile_array.map(d => d.name);
}
function getNamesOfTablesInArray(table_array) {
checkQObjectArrayType(table_array, "Table");
return table_array.map(t => t.name);
}
// Remove any Text Variables from the input array
function removeTextVariablesFromArray(variable_array) {
const num_vars = variable_array.length;
for (let j = num_vars - 1; j > -1; j--) {
if (variable_array[j].variableType == 'Text') {
variable_array.splice(j, 1);
}
}
}