Hi andthanks in advance for your help.
I think I'm crashing of my javascript weaknesses, and I'm always trying to avoid functions.
In this case I am injecting a JSON with with one object, and multiple objects.
For one object I can suimply use the range node, for the array I'm (failing) in using a function.
My questions:
- how would the correct function be?
- how could I use the change node instead of a function, using JSONata?
Feel free to point me to any helpful online documentation
[{"id":"aa288e5a.6ead2","type":"inject","z":"63ccda68.bf7a74","name":"insert multiple data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"id\":1,\"first_name\":\"Mariette\",\"last_name\":\"Ulrik\",\"email\":\"mulrik0@gravatar.com\",\"gender\":\"Genderqueer\",\"presence\":75,\"rating\":1,\"experience\":\"Yellow\"},{\"id\":2,\"first_name\":\"Gaspar\",\"last_name\":\"Dunne\",\"email\":\"gdunne1@google.com.au\",\"gender\":\"Agender\",\"presence\":46,\"rating\":3,\"experience\":\"Violet\"},{\"id\":3,\"first_name\":\"Amargo\",\"last_name\":\"Pien\",\"email\":\"apien2@reuters.com\",\"gender\":\"Agender\",\"presence\":95,\"rating\":2,\"experience\":\"Turquoise\"},{\"id\":4,\"first_name\":\"Sylvester\",\"last_name\":\"Dunn\",\"email\":\"sdunn3@abc.net.au\",\"gender\":\"Polygender\",\"presence\":61,\"rating\":1,\"experience\":\"Mauv\"},{\"id\":5,\"first_name\":\"Abbie\",\"last_name\":\"Sharrock\",\"email\":\"asharrock4@reverbnation.com\",\"gender\":\"Genderfluid\",\"presence\":21,\"rating\":1,\"experience\":\"Purple\"},{\"id\":6,\"first_name\":\"Leticia\",\"last_name\":\"Grouvel\",\"email\":\"lgrouvel5@purevolume.com\",\"gender\":\"Female\",\"presence\":38,\"rating\":5,\"experience\":\"Indigo\"},{\"id\":7,\"first_name\":\"Alberta\",\"last_name\":\"Goodfellowe\",\"email\":\"agoodfellowe6@un.org\",\"gender\":\"Female\",\"presence\":78,\"rating\":3,\"experience\":\"Violet\"},{\"id\":8,\"first_name\":\"Alair\",\"last_name\":\"Mindenhall\",\"email\":\"amindenhall7@issuu.com\",\"gender\":\"Non-binary\",\"presence\":30,\"rating\":5,\"experience\":\"Turquoise\"},{\"id\":9,\"first_name\":\"Ulises\",\"last_name\":\"Millan\",\"email\":\"umillan8@symantec.com\",\"gender\":\"Genderfluid\",\"presence\":2,\"rating\":2,\"experience\":\"Purple\"},{\"id\":10,\"first_name\":\"Man\",\"last_name\":\"Brittian\",\"email\":\"mbrittian9@mashable.com\",\"gender\":\"Polygender\",\"presence\":16,\"rating\":2,\"experience\":\"Indigo\"}]","payloadType":"json","x":630,"y":1120,"wires":[["9c83cf10.93e0c"]]},{"id":"e09ad7d8.ebc788","type":"inject","z":"63ccda68.bf7a74","name":"insert one data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"id\":8,\"first_name\":\"Alair\",\"last_name\":\"Mindenhall\",\"email\":\"amindenhall7@issuu.com\",\"gender\":\"Non-binary\",\"presence\":30,\"rating\":5,\"experience\":\"Turquoise\"}","payloadType":"json","x":640,"y":1160,"wires":[["af6577ee.d630b8"]]},{"id":"af6577ee.d630b8","type":"range","z":"63ccda68.bf7a74","minin":"1","maxin":"5","minout":"1","maxout":"1000","action":"scale","round":true,"property":"payload.rating","name":"Map one \"rating\"","x":840,"y":1160,"wires":[["5c57d753.c27ed8"]]},{"id":"5c57d753.c27ed8","type":"debug","z":"63ccda68.bf7a74","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1050,"y":1140,"wires":[]},{"id":"9c83cf10.93e0c","type":"function","z":"63ccda68.bf7a74","name":"Map all \"ratings\"","func":"// Javascript doesn't have built-in support for ranges\n// Insted we use arrays of two elements to represent ranges\n\n// Javascript doesn't have built-in support for ranges\n// Insted we use arrays of two elements to represent ranges\nvar mapRange = function(from, to, s) {\n return to[0] + (s - from[0]) * (to[1] - to[0]) / (from[1] - from[0]);\n};\n \noutput = {};\n\nfor (var i of msg.payload) {\n output[i].rating = mapRange([0, 10], [-1, 0], i.rating);\n}\nmsg.payload = output;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":840,"y":1120,"wires":[["5c57d753.c27ed8"]]}]