QScript Functions for Processing Arrays

From Q
Jump to navigation Jump to search

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('JavaScript Text Analysis Functions');
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(function(q) { return q.name; });
}

function getNamesOfQuestionsAndDataFilesInArray(question_array) {
    checkQObjectArrayType(question_array, "Question");
    return question_array.map(function(q) { return 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(function(v) { return 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(function(v) { return 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(function(v) { return v.name + ": " + v.label; });
}

function getQuestionNamesAndLabelsOfVariablesInArray(variable_array) {
    checkQObjectArrayType(variable_array, "Variable");
    return variable_array.map(function(v) { return getQuestionNameAndVariableLabel(v); });
}

function getQuestionNamesAndLabelsAndDataFilesOfVariablesInArray(variable_array) {
    checkQObjectArrayType(variable_array, "Variable");
    return variable_array.map(function(v) { return getQuestionNameAndVariableLabel(v) + ' [' + v.question.dataFile.name + ']'; });
}

function getNamesOfDataFilesInArray(datafile_array) {
    checkQObjectArrayType(datafile_array, "DataFile");
    return datafile_array.map(function(d) { return d.name; });
}

function getNamesOfTablesInArray(table_array) {
    checkQObjectArrayType(datafile_array, "Table");
    return table_array.map(function(t) {return t.name; });
}

// Remove any Text Variables from the input array
function removeTextVariablesFromArray(variable_array) {
    var num_vars = variable_array.length;
    for (var j = num_vars - 1; j > -1; j--) {
        if (variable_array[j].variableType == 'Text')
            variable_array.splice(j,1);
    }
}

See also