rwkiii
rwkiii

Reputation: 5846

Error creating JSON object consisting of Object.string

I have a jQuery object that contains strings. I use it in many places with no problems.

The object (for example):

resources {
    res1: "Resource 1",
    res2: "Resource 2",
    ... etc ...
}

I use this object like this:

ctl.text(resources.res1);

This works fine, but now I need to use these string resources like:

options {
    resources.res1: "Some string",
    resources.res2: "Another string"
}

In the above code I get an error starting at .res1 stating that a : is expected. Since resources.res1 contains a string I thought this should be valid. How can I use resources.res1 when creating options {}?

Upvotes: 0

Views: 52

Answers (3)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

you need to create options object first, then push in the values, as:

var options = {};
options[resources['res1']] = "Some String";
options[resources['res2']] = "Aonther String";
console.log(options);
//gives
// Object { Resource 1="Some String", Resource 2="Aonther String"}

Upvotes: 1

elzi
elzi

Reputation: 5672

Why not do

options = {
    resources : {
        res1: "Some string",
        res2: "Another string"
    }
}

Upvotes: 0

artm
artm

Reputation: 8584

You're doing it the wrong way around, if you're trying to set options values from resources you need:

options {
    val1: resources.res1,
    val2: resources.res2
}

Upvotes: 1

Related Questions