Ajility
Ajility

Reputation: 567

Programmatic JSON Conditionals & Comparisons across Object Properties

The pseudo-code for the conditionals I'm trying to create:

this.tagSet.forEach((obj) => {

    var tagData = {
        name: obj.tag.name,
        "if": { obj.tag.units: "bool" },
        "then": { "yValueFormatString": "#,##0" },
        "else": {
            "then": { "yValueFormatString": "#,##0.0 " + obj.tag.units }
        },
    };

    tagDataCollection.push(tagData);
});

If obj.tag.units == "bool" then yValueFormatString property should be a certain value. Else, another value.

I couldn't find much info in the standard.

Upvotes: 0

Views: 27

Answers (1)

ABGR
ABGR

Reputation: 5235

You could do something like this:

 this.tagSet.forEach((obj) => {
      var temp = obj.tag.units === "bool" ? "#,##0" :"#,##0.0 " + obj.tag.units 
        var tagData = {
            name: obj.tag.name,
            yValueFormatString: temp
        };

        tagDataCollection.push(tagData);
    });

Or, even this would do just as well:

        var yValueFormatString = obj.tag.units === "bool" ? "#,##0" :"#,##0.0 " + 
           obj.tag.units 
            var tagData = {
                name: obj.tag.name,
                yValueFormatString
            };

var a = "foo";

var temp = a=== "foo" ? "something" : "something else"
var obj = {b: "bar", temp};

console.log(obj)

Upvotes: 1

Related Questions