Alternatives to eval()

Use RED.util.getObjectProperty to get sub properties of an object using a string

var list = { "find": { "me": [{ "in": { "this": "section" } }, { "in": { "this": "room" } }] } };
var listArray = list.find.me;
var location = "in.this";
var location2a = "in";
var location2b = "this";
var string = "section";

for (let i = 0; i < listArray.length; i++) {
    if (listArray[i].in.this == "section") {
        node.warn("found validation");
        //execute something
    }

    if (RED.util.getObjectProperty(listArray[i], location) == string) {
        node.warn("found using msg strings");
        //execute something
    }

    if (listArray[i][location2a][location2b] == string) {
        node.warn("alternative found using msg strings");
        //execute something
    }
}
1 Like