# Using object keys: alternatives to this code?

**URL:** <https://discourse.nodered.org/t/using-object-keys-alternatives-to-this-code/18031>\
**Category:** General\
**Created:** [16 November 2019 08:12 UTC](https://discourse.nodered.org/t/using-object-keys-alternatives-to-this-code/18031 "2019-11-16T08:12:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![danleighton](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danleighton/32/7711_2.png) [@danleighton](https://discourse.nodered.org/u/danleighton)\
**Post date:** [16 November 2019 08:12 UTC](https://discourse.nodered.org/t/using-object-keys-alternatives-to-this-code/18031/1 "2019-11-16T08:12:49Z")

</div>

Hi all, I'd like to do this with nodes, rather than using a JS function node.

Why? Because it's much easier for non-programmers to understand the flow than the JS 🙂

Essentially, I am using a known key, stored in `flow.foo`, to extract the data from an object `flow.bar`of this structure:

```auto
{
        "itemA": {
            "amount": "20",
            "type": "hamper"
        },
        "itemB": {
            "amount": "22"
            "type": "box"
}

```

Currently I have this JS in a Function node.

```auto
var foo = flow.get("foo");
var bar = flow.get("bar");
msg.payload = bar[foo];
return msg;

```

So assuming flow.foo = "Item A"  
msg.payload will return:

```auto
{
    "payload": {
        "amount": "20",
        "type": "hamper"
    }
}

```

Originally I had tried many variants on a change node like this... but no luck:

![53](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f929a2d76d1612a4706ffccf8b2b9789888f0b21.png)

Any help appreciated!

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [16 November 2019 08:58 UTC](https://discourse.nodered.org/t/using-object-keys-alternatives-to-this-code/18031/2 "2019-11-16T08:58:13Z")

</div>

With your Change node, if you change the type of the "to" field to Expression, you can use:

```auto
$lookup($flowContext("bar"), $flowContext("foo"))

```

Whether that is any more readable then the Function code is going to be subjective.

---

<div class="post-metadata">

**Author:** ![danleighton](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danleighton/32/7711_2.png) [@danleighton](https://discourse.nodered.org/u/danleighton)\
**Post date:** [16 November 2019 09:46 UTC](https://discourse.nodered.org/t/using-object-keys-alternatives-to-this-code/18031/3 "2019-11-16T09:46:11Z")

</div>

Ah! I knew Andrew would have put in some way to do it in Jsonata 🙂

It is probably slightly more readable, IMO, yes.

Would you mind me asking why what I attempted to do is not possible?

`flow.bar[flow.foo]`

I suspect the answer is that JS doesn’t work like that... but my question is really, why doesn’t it work like that? It feels like a reasonable syntactic choice!

(TBH `flow.bar.flow.foo` or `flow.bar(flow.foo)` feels even better, but probably good reasons they aren’t good!)

---

<div class="post-metadata">

**Author:** ![danleighton](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danleighton/32/7711_2.png) [@danleighton](https://discourse.nodered.org/u/danleighton)\
**Post date:** [16 November 2019 09:47 UTC](https://discourse.nodered.org/t/using-object-keys-alternatives-to-this-code/18031/4 "2019-11-16T09:47:21Z")

</div>

And btw, just to say here publicly:

Node Red, awesome. My personal thanks to you for your amazing work these past few years. Hugely appreciated!

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [16 November 2019 09:57 UTC](https://discourse.nodered.org/t/using-object-keys-alternatives-to-this-code/18031/5 "2019-11-16T09:57:12Z")

</div>

Who’s Andrew ? 😉

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [16 November 2019 10:10 UTC](https://discourse.nodered.org/t/using-object-keys-alternatives-to-this-code/18031/6 "2019-11-16T10:10:28Z")

</div>

> [@danleighton](#):
>
> `flow.bar[flow.foo]`

The 'flow' input type simply doesn't support that type of self reference when evaluating a property name.

---

<div class="post-metadata">

**Author:** ![danleighton](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danleighton/32/7711_2.png) [@danleighton](https://discourse.nodered.org/u/danleighton)\
**Post date:** [16 November 2019 10:11 UTC](https://discourse.nodered.org/t/using-object-keys-alternatives-to-this-code/18031/7 "2019-11-16T10:11:03Z")

</div>

Sorry Andrew Coleman is the guy who wrote Jsonata! Awesome individual. It is so useful!

🙂

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [16 November 2019 10:11 UTC](https://discourse.nodered.org/t/using-object-keys-alternatives-to-this-code/18031/8 "2019-11-16T10:11:58Z")

</div>

> [@danleighton](#):
>
> Would you mind me asking why what I attempted to do is not possible?
> 
> `flow.bar[flow.foo]`

Well it's all down to context again (in the wider programming sense). If there was an object called flow that was accessible then yes you could do that... in fact if you had it all stored on the `msg` then you could have. As flow. (and global.) have to be special (so flow properties are indeed per flow (or subflow)), then they are not simple objects so `flow.bar` is not the same as flow.get("bar") . The reason for using .get and .set is so that these methods can be extended to external storage for persistence.

---

<div class="post-metadata">

**Author:** ![danleighton](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danleighton/32/7711_2.png) [@danleighton](https://discourse.nodered.org/u/danleighton)\
**Post date:** [16 November 2019 10:13 UTC](https://discourse.nodered.org/t/using-object-keys-alternatives-to-this-code/18031/9 "2019-11-16T10:13:46Z")

</div>

Ahh! Now that makes sense! I knew there had to be a reason! Thank you 🙂
