# Alternatives to eval()

**URL:** https://discourse.nodered.org/t/alternatives-to-eval/50550
**Category:** General
**Created:** [2 September 2021 20:40 UTC](https://discourse.nodered.org/t/alternatives-to-eval/50550 "2021-09-02T20:40:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![FlangeMonkey](https://avatars.discourse-cdn.com/v4/letter/f/e274bd/32.png) [@FlangeMonkey](https://discourse.nodered.org/u/FlangeMonkey)
#### Post date: [2 September 2021 20:40 UTC](https://discourse.nodered.org/t/alternatives-to-eval/50550/1 "2021-09-02T20:40:00Z")

</div>

Hi Guys,

I have a need for a dynamic object defined by a msg.topic within an array and put together the following for testing. Both the solutions for using eval() and object definitions using square brackets work.

However, is there a better way to do this? Baring in mind a completely different solution may be better, but I'm still curious about replacements for eval().

Thanks,

```auto
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 (i = 0; i < listArray.length; i++) {
    if (listArray[i].in.this == "section") {
        node.warn("found validation");
        //execute something
    }
    
    if (eval("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
    }
}

return msg;

```

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [2 September 2021 20:51 UTC](https://discourse.nodered.org/t/alternatives-to-eval/50550/2 "2021-09-02T20:51:39Z")

</div>

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

```auto
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
    }
}

```

---

<div class="post-metadata">

### Author: ![FlangeMonkey](https://avatars.discourse-cdn.com/v4/letter/f/e274bd/32.png) [@FlangeMonkey](https://discourse.nodered.org/u/FlangeMonkey)
#### Post date: [3 September 2021 17:17 UTC](https://discourse.nodered.org/t/alternatives-to-eval/50550/3 "2021-09-03T17:17:28Z")

</div>

> [@Steve-Mcl](#):
>
> Use `RED.util.getObjectProperty` to get sub properties of an object using a string

Dude you rock, thank you very much. I also wasn't aware of this util module so that's something to explore.

Generally speaking are there any other alternates to eval()?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [3 September 2021 17:22 UTC](https://discourse.nodered.org/t/alternatives-to-eval/50550/4 "2021-09-03T17:22:17Z")

</div>

If you are using Monaco editor, type `RED.util.` and you will see all util functions.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [17 September 2021 17:23 UTC](https://discourse.nodered.org/t/alternatives-to-eval/50550/5 "2021-09-17T17:23:03Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
