3

I want to define a variable that contains a variable. In this example the variable that I want to pass is "filldata".

I have tried passing it using $filldata as well as +filldata+.

if (fill == true) {
     $filldata = "fill: true,";
} else {
     $filldata = "fill: false,";
};

if (amount == true) {
        var set1 = {
            label: "Earnings",
            id: "earnings",
            data: data1,
            points: {
                show: true,
            },
            bars: {
                show: false,
                barWidth: 12,
                aling: 'center'
            },
            lines: {
                show: true,
                $filldata
            },
            yaxis: 1
        };
    } else {
        var set1 = "";
    }
6
  • var a = b, b = c, c = 1;? I don't understand?.. Commented Nov 4, 2016 at 20:59
  • 1
    You want to dynamically set a property on an object. In your example you can simply do lines: {show: true, fill: fill}. Alternative you can assign the name and the value to variables (e.g. key = 'fill'; value = true) and do lines: {show: true, [key]: value}. Commented Nov 4, 2016 at 20:59
  • 1
    Instead of using $filldata in your set object as you are now, why not just do fill: $filldata and set $filldata to true or false? Commented Nov 4, 2016 at 21:00
  • @FelixKling - Thank you! Your answer worked using - fill:fill. The answer below did not work since it does not work if fill=false. If you want to make an answer I will choose it as correct. Commented Nov 4, 2016 at 21:07
  • if (amount == true) is the same as if (amount) Commented Nov 4, 2016 at 21:07

1 Answer 1

2

Since you are just trying to create a boolean property named 'fill' with the value of some variable, also called fill (using fussy truthy/falsy values), then you can just skip creating the intermediate $filldata variable altogether and just create the property with the value evaluated inline. It's more succinct and more obvious.

Try:

if (amount == true) {
    var set1 = {
        label: "Earnings",
        id: "earnings",
        data: data1,
        points: {
            show: true,
        },
        bars: {
            show: false,
            barWidth: 12,
            aling: 'center'
        },
        lines: {
            show: true,
            fill: fill==true
        },
        yaxis: 1
    };
} else {
    var set1 = "";
}

EDIT:

Also, note that it is not good practice to declare the variable set1 inside the if block scope if you intend to use it elsewhere. A better alternative would be:

var set1 = (amount == true) ?
    {...your object as defined above...}
    : "";
Sign up to request clarification or add additional context in comments.

1 Comment

It would be more useful if you provided an explanation of the problem/solution, not just code and "try".

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.