1

I am trying to build this function that sets up my object for me

var schema = function(tableName, data) {
    return dataSet = {
        tableName: {
            1: data
        }
    };
};
var dataSet = schema("messages", data);

But when I execute this it returns tableName as a string, not using the variable that I pass through the function?

Is it possible to use the variable that I pass into my function as a name so that I get it returned like this:

{
    "message": {
        "1": {
            "username": "Simon",
            "message": "First message"
        }
    }
}

Instead of this:

{
    "tableName": {
        "1": {
            "username": "Simon",
            "message": "First message"
        }
    }
}

1 Answer 1

5

Dot notation is not evaluated, but the square bracket syntax is:

var schema = function(tableName, data) {
    var dataSet = {};
    dataSet[tableName] = {
        1: data
    };
    return dataSet;
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.