Rename Key in Object

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.

[{"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"]]}]

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.

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

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.

[{"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"]]}]

Thanks for the reply.

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

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

1 Like

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

1 Like

Thanks for the clarification

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