# Rename Key in Object

**URL:** <https://discourse.nodered.org/t/rename-key-in-object/46628>\
**Category:** General\
**Created:** [3 June 2021 06:29 UTC](https://discourse.nodered.org/t/rename-key-in-object/46628 "2021-06-03T06:29:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![softy2k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/softy2k/32/23707_2.png) [@softy2k](https://discourse.nodered.org/u/softy2k)\
**Post date:** [3 June 2021 06:29 UTC](https://discourse.nodered.org/t/rename-key-in-object/46628/1 "2021-06-03T06:29:48Z")

</div>

Hello

I try to rename a key from an Object, but keeping the order of the keys (index).

If the new key is text (string), everything is OK.  
But, if the new key is a number (although it is declared as a string), the new key moves to the first position in the index.

I tried various options, but I can't get the desired result.

```auto
[{"id":"92465edc.815478","type":"debug","z":"821f0ea6.3f40c","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":990,"y":2000,"wires":[]},{"id":"60f2794a.5d981","type":"inject","z":"821f0ea6.3f40c","name":"Go","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":600,"y":2000,"wires":[["5f38886b.237468"]]},{"id":"5f38886b.237468","type":"function","z":"821f0ea6.3f40c","name":"RenameKeys","func":"let obj = { \n COL1: \"A\",\n COL2: \"B\",\n COL3: \"C\"\n};\n\nlet keys = Object.keys(obj)\nmsg.oldKeys = keys // debug only\n\n\nlet oldKey = \"COL2\";\n//let newKey = \"xxx\"; // OK [\"COL1\",\"xxx\",\"COL3\"]\nlet newKey = \"2\"; // NOT OK [\"2\",\"COL1\",\"COL3\"]\n\n\n\nlet newObj = {};\n\nkeys.forEach( e => {\n if (e == oldKey) newObj[newKey] = obj[e]\n else newObj[e] = obj[e]\n})\n\nmsg.newKeys = Object.keys(newObj) // debug obly\n\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":800,"y":2000,"wires":[["92465edc.815478"]]}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [3 June 2021 06:42 UTC](https://discourse.nodered.org/t/rename-key-in-object/46628/2 "2021-06-03T06:42:16Z")

</div>

I see the original names are "COL1" etc are they comimg from the csv node?

If so you can give them names in the csv node. set a comma separated list of keys you wish.

---

<div class="post-metadata">

**Author:** ![softy2k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/softy2k/32/23707_2.png) [@softy2k](https://discourse.nodered.org/u/softy2k)\
**Post date:** [3 June 2021 06:43 UTC](https://discourse.nodered.org/t/rename-key-in-object/46628/3 "2021-06-03T06:43:33Z")

</div>

It's just a coincidence, it has nothing to do with CSV

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [3 June 2021 07:09 UTC](https://discourse.nodered.org/t/rename-key-in-object/46628/4 "2021-06-03T07:09:04Z")

</div>

Ok it seems that the function node sorts the object.

The change node does not but if you want the $keys() it does sort them when you apply keys.  
notice the payload objects in the two different methods.

```auto
[{"id":"5f38886b.237468","type":"function","z":"c74669a0.6a34f8","name":"RenameKeys","func":"let obj = { \n COL1: \"A\",\n COL2: \"B\",\n COL3: \"C\"\n};\n\nlet keys = Object.keys(obj)\nmsg.oldKeys = keys // debug only\n\n\nlet oldKey = \"COL2\";\n//let newKey = \"xxx\"; // OK [\"COL1\",\"xxx\",\"COL3\"]\nlet newKey = \"2\"; // NOT OK [\"2\",\"COL1\",\"COL3\"]\n\n\n\nlet newObj = {};\n\nkeys.forEach( e => {\n if (e == oldKey) Object.assign(newObj,{[newKey]:obj[e]})\n else Object.assign(newObj ,{[e]:obj[e]})\n})\nmsg.payload = newObj;\nmsg.newKeys = Object.keys(newObj) // debug obly\n\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":3920,"wires":[["92465edc.815478"]]},{"id":"60f2794a.5d981","type":"inject","z":"c74669a0.6a34f8","name":"Go","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"COL1\": \"A\", \"COL2\": \"B\", \"COL3\": \"C\" }","payloadType":"json","x":130,"y":3920,"wires":[["5f38886b.237468","1139e721.623721"]]},{"id":"92465edc.815478","type":"debug","z":"c74669a0.6a34f8","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":520,"y":3920,"wires":[]},{"id":"1139e721.623721","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$each(payload, function($v,$k){\t$k = \"COL2\" ? {\"2\":$v} : {$k:$v}\t})","tot":"jsonata"},{"t":"set","p":"newkeys","pt":"msg","to":"$keys(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":4000,"wires":[["92465edc.815478"]]}]

```

---

<div class="post-metadata">

**Author:** ![softy2k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/softy2k/32/23707_2.png) [@softy2k](https://discourse.nodered.org/u/softy2k)\
**Post date:** [3 June 2021 07:19 UTC](https://discourse.nodered.org/t/rename-key-in-object/46628/5 "2021-06-03T07:19:03Z")

</div>

Thanks for the reply.

But how could it be done only in pure javascript, without other nodes?

---

<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:** [3 June 2021 07:24 UTC](https://discourse.nodered.org/t/rename-key-in-object/46628/6 "2021-06-03T07:24:56Z")

</div>

The order of keys in an object is not fixed. If you need a fixed order you need to use an array..

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [3 June 2021 07:28 UTC](https://discourse.nodered.org/t/rename-key-in-object/46628/7 "2021-06-03T07:28:12Z")

</div>

Objects really have no order they can come in any order and are not guaranteed to stay in order, so I don't think you can do it.

I would work round the issue by giving key as "\_2" of " 2" (first is in convention second is not but works). You can remove the first character when dispalyiing etc

---

<div class="post-metadata">

**Author:** ![softy2k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/softy2k/32/23707_2.png) [@softy2k](https://discourse.nodered.org/u/softy2k)\
**Post date:** [3 June 2021 07:30 UTC](https://discourse.nodered.org/t/rename-key-in-object/46628/8 "2021-06-03T07:30:25Z")

</div>

Thanks for the clarification

---

<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:** [2 August 2021 07:31 UTC](https://discourse.nodered.org/t/rename-key-in-object/46628/9 "2021-08-02T07:31:13Z")

</div>

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