# How to input a same property into an array

**URL:** <https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791>\
**Category:** General\
**Created:** [27 February 2023 07:32 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791 "2023-02-27T07:32:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [27 February 2023 07:32 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/1 "2023-02-27T07:32:33Z")

</div>

Hello, Im a newbie here, I have a question with my sample json file(payload) in the image below. I want to input a property WindSpeed into the payload.Weather. I could do it manually like using a change node and set payload.Weather[0].WindSpeed upto the index[4]. But how can i put simultaneously? Thanks in advance for the help.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/ca9679329e464870146252fe8d1fcf62037bbb63.png)

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [27 February 2023 07:33 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/2 "2023-02-27T07:33:08Z")

</div>

This is my desired result. Windspeed below the "Day" value.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/7/374414ab3cae4078cb127c8c54f35586c1244150.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 February 2023 09:47 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/3 "2023-02-27T09:47:27Z")

</div>

If you want to do it to the whole array then you can so it in a Function node using `Array.forEach()`. Something like

```auto
msg.payload.Weather.forEach(element => element.WindSpeed = "3.5");
return msg

```

If you just want to do it on a part of the array then

```auto
for (let i = 0; i <= 4; i++) {
  msg.payload.Weather[i] = "3.5"
}
return msg

```

---

<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:** [27 February 2023 18:21 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/4 "2023-02-27T18:21:45Z")

</div>

To do so in a change node with JSONata  
`$$.payload ~> |Weather|{"WindSpeed": "3.5"}|`  
"3.5" can be a msg property.  
[edit] for future readers, to delete the property `Today` it would be  
`$$.payload ~> |Weather|{},"Today"|`  
Where the array `"Today"` can be an array of proerty names. You can add and delete at same time.  
`$$.payload ~> |Weather|{"WindSpeed": "3.5"}, "Today"|`

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [28 February 2023 08:15 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/5 "2023-02-28T08:15:28Z")

</div>

Thanks for your answers. I will try.

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [22 March 2023 07:24 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/6 "2023-03-22T07:24:58Z")

</div>

Hi @E1cid thanks for your answer I was able to input property in the array. However Is it possible to input a property from a different msg.property? Like in this input json file

```auto
{
	"Weather": [
		{
			"Day": "Monday",
			"Today": "Nice",
			"Tomorrow": "ebasdva",
			"Nextweek": "sdvaerg"
		},
		{
			"Day": "Tuesday",
			"Today": "Good",
			"Tomorrow": "raersebeb",
			"Nextweek": ""
		},
		{
			"Day": "Wednesday",
			"Today": "Very Good",
			"Tomorrow": "argarwg",
			"Nextweek": "awergawgr"
		},
		{
			"Day": "Thursday",
			"Today": "Nice",
			"Tomorrow": "argarg",
			"Nextweek": ""
		},
		{
			"Day": "Friday",
			"Today": "eweffw34",
			"Tomorrow": "32fevd",
			"Nextweek": "32feb"
		}
	],
	"WeekNumber": [
		{
			"day": "Monday",
			"DayNumber": "01"
		},
		{
			"day": "Tuesday",
			"DayNumber": "02"
		},
		{
			"day": "Wednesday",
			"DayNumber": "03"
		},
		{
			"day": "Thursday",
			"DayNumber": "04"
		},
		{
			"day": "Friday",
			"DayNumber": "05"
		}
	]
}

```

If the property Weather.Day matches with WeekNumber.day, it will input the data of WeekNumber.day to a new property Weather.DayNumber. Like in the json below which is my desired outcome.

```auto
{
	"Weather": [
		{
			"Day": "Monday",
			"DayNumber": "01",
			"Today": "Nice",
			"Tomorrow": "ebasdva",
			"Nextweek": "sdvaerg"
		},
		{
			"Day": "Tuesday",
			"DayNumber": "02",
			"Today": "Good",
			"Tomorrow": "raersebeb",
			"Nextweek": ""
		},
		{
			"Day": "Wednesday",
			"DayNumber": "03",
			"Today": "Very Good",
			"Tomorrow": "argarwg",
			"Nextweek": "awergawgr"
		},
		{
			"Day": "Thursday",
			"DayNumber": "04",
			"Today": "Nice",
			"Tomorrow": "argarg",
			"Nextweek": ""
		},
		{
			"Day": "Friday",
			"DayNumber": "05",
			"Today": "eweffw34",
			"Tomorrow": "32fevd",
			"Nextweek": "32feb"
		}
	]
	
}

```

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [22 March 2023 07:25 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/7 "2023-03-22T07:25:37Z")

</div>

Thanks in advance for your reply. I am trying to learn.

---

<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:** [22 March 2023 07:40 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/8 "2023-03-22T07:40:50Z")

</div>

Is weeknumber array generated by you or returned from some api?  
Also is weather array in msg or payload?  
Do you simply want a daynumber in all objects that matches the Day?

Best to supply an example flow with data set in inject nodes to simulate what is actually happening, other wise we are left to fill in the gaps, which is not always correct.

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [22 March 2023 07:56 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/9 "2023-03-22T07:56:05Z")

</div>

WeekNumber is an array from an API. Weather is from payload. Yes exactly, I want the daynumber in all objects that matches the day.

---

<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:** [22 March 2023 08:09 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/10 "2023-03-22T08:09:38Z")

</div>

Try

```auto
(
  $daylookup := $$.payload.WeekNumber.${$.day: $.DayNumber};
  $$.payload ~> |$.Weather|{"DayNumber": $lookup($daylookup,$.Day)}|
)

```

The first line creates a look up object.

```auto
{
  "Monday": "01",
  "Tuesday": "02",
  etc.......
}

```

The second line adds the property using the lookup object

Flow example

```auto
[{"id":"05232c7e552429bd","type":"inject","z":"65617ffeb779f51c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"Weather\":[{\"Day\":\"Monday\",\"Today\":\"Nice\",\"Tomorrow\":\"ebasdva\",\"Nextweek\":\"sdvaerg\"},{\"Day\":\"Tuesday\",\"Today\":\"Good\",\"Tomorrow\":\"raersebeb\",\"Nextweek\":\"\"},{\"Day\":\"Wednesday\",\"Today\":\"Very Good\",\"Tomorrow\":\"argarwg\",\"Nextweek\":\"awergawgr\"},{\"Day\":\"Thursday\",\"Today\":\"Nice\",\"Tomorrow\":\"argarg\",\"Nextweek\":\"\"},{\"Day\":\"Friday\",\"Today\":\"eweffw34\",\"Tomorrow\":\"32fevd\",\"Nextweek\":\"32feb\"}],\"WeekNumber\":[{\"day\":\"Monday\",\"DayNumber\":\"01\"},{\"day\":\"Tuesday\",\"DayNumber\":\"02\"},{\"day\":\"Wednesday\",\"DayNumber\":\"03\"},{\"day\":\"Thursday\",\"DayNumber\":\"04\"},{\"day\":\"Friday\",\"DayNumber\":\"05\"}]}","payloadType":"json","x":330,"y":4400,"wires":[["e1adeee7fae3b8ed"]]},{"id":"e1adeee7fae3b8ed","type":"change","z":"65617ffeb779f51c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t $daylookup := $$.payload.WeekNumber.${$.day: $.DayNumber};\t $$.payload ~> |$.Weather|{\"DayNumber\": $lookup($daylookup,$.Day)}|\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":4400,"wires":[["17f95f5b251d8b43"]]},{"id":"17f95f5b251d8b43","type":"debug","z":"65617ffeb779f51c","name":"debug 250","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":4400,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [22 March 2023 09:02 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/11 "2023-03-22T09:02:59Z")

</div>

Thanks a lot @E1cid it worked on my flow. But i have one last question,  
the result is like this

```auto
{
			"Day": "Friday",
			"Today": "eweffw34",
			"Tomorrow": "32fevd",
			"Nextweek": "32feb"
			"DayNumber": "05"
		}

```

how can i make it

```auto
{
			"Day": "Friday",
			"DayNumber": "05",
			"Today": "eweffw34",
			"Tomorrow": "32fevd",
			"Nextweek": "32feb"
		}

```

The DayNumber goes on the 2nd line.

---

<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:** [22 March 2023 09:08 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/12 "2023-03-22T09:08:51Z")

</div>

The order of objects do not matter, please explain why you want that order. As objects are orderless. It could be done, but seems pointless.

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [22 March 2023 09:10 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/13 "2023-03-22T09:10:56Z")

</div>

Because the output requires a format. I know in json it does not matter, but it is a response to a client API and needs a proper format 🙏

And if you could lead me to proper resources where I can study more of it. It seems the documantation in [Jsonata.org](http://Jsonata.org) does not have this things.

---

<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:** [22 March 2023 09:12 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/14 "2023-03-22T09:12:50Z")

</div>

> [@kjbambe](#):
>
> client API and needs a proper format

I doubt that the object order will affect the api, have you tried it as is?

> [@kjbambe](#):
>
> [Jsonata.org](http://Jsonata.org) does not have this things.

Please define `things` does it mean the transform syntax `|...|...|`if so [Other Operators · JSONata](https://docs.jsonata.org/other-operators#-------transform)  
Or parenthesized expressions blocks `(...; ...)`[Query composition · JSONata](https://docs.jsonata.org/composition#parenthesized-expressions-and-blocks)  
Or variable binding `:=` [Other Operators · JSONata](https://docs.jsonata.org/other-operators#-variable-binding)

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [23 March 2023 02:13 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/15 "2023-03-23T02:13:29Z")

</div>

Thanks for the links I haven't got thru them yet. I will study more.

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [23 March 2023 02:21 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/16 "2023-03-23T02:21:07Z")

</div>

```auto
I doubt that the object order will affect the api, have you tried it as is?

```

Yes i have tried as it is and i got a comment to redo the order of the objects.

---

<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:** [23 March 2023 07:18 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/17 "2023-03-23T07:18:21Z")

</div>

Could you you provide the error received

Here is how you will have to do it to maintain the order

```auto
[{"id":"bacc35bea7d25bee","type":"inject","z":"8be23377afc3327e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \t\"Weather\": [\t\t{ \t\t\t\"Day\": \"Monday\", \t\t\t\"Today\": \"Nice\", \t\t\t\"Tomorrow\": \"ebasdva\", \t\t\t\"Nextweek\": \"sdvaerg\" \t\t}, \t\t{ \t\t\t\"Day\": \"Tuesday\", \t\t\t\"Today\": \"Good\", \t\t\t\"Tomorrow\": \"raersebeb\", \t\t\t\"Nextweek\": \"\" \t\t}, \t\t{ \t\t\t\"Day\": \"Wednesday\", \t\t\t\"Today\": \"Very Good\", \t\t\t\"Tomorrow\": \"argarwg\", \t\t\t\"Nextweek\": \"awergawgr\" \t\t}, \t\t{ \t\t\t\"Day\": \"Thursday\", \t\t\t\"Today\": \"Nice\", \t\t\t\"Tomorrow\": \"argarg\", \t\t\t\"Nextweek\": \"\" \t\t}, \t\t{ \t\t\t\"Day\": \"Friday\", \t\t\t\"Today\": \"eweffw34\", \t\t\t\"Tomorrow\": \"32fevd\", \t\t\t\"Nextweek\": \"32feb\" \t\t} \t], \t\"WeekNumber\": [\t\t{ \t\t\t\"day\": \"Monday\", \t\t\t\"DayNumber\": \"01\" \t\t}, \t\t{ \t\t\t\"day\": \"Tuesday\", \t\t\t\"DayNumber\": \"02\" \t\t}, \t\t{ \t\t\t\"day\": \"Wednesday\", \t\t\t\"DayNumber\": \"03\" \t\t}, \t\t{ \t\t\t\"day\": \"Thursday\", \t\t\t\"DayNumber\": \"04\" \t\t}, \t\t{ \t\t\t\"day\": \"Friday\", \t\t\t\"DayNumber\": \"05\" \t\t} \t] }","payloadType":"json","x":390,"y":320,"wires":[["cbd8285fc385a66c"]]},{"id":"cbd8285fc385a66c","type":"change","z":"8be23377afc3327e","name":"","rules":[{"t":"set","p":"payload.Weather","pt":"msg","to":"( \t$daylookup := $$.payload.WeekNumber.${$.day: $.DayNumber}; \t$$.payload.Weather.{\t \"Day\": $.Day,\t \"DayNumber\":$lookup($daylookup, $.Day),\t \"Today\": $.Today,\t \"Tomorrow\": $.Tomorrow,\t \"Nextweek\": $.Nextweek\t}\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":280,"wires":[["a0b3732a8e570fe9"]]},{"id":"a0b3732a8e570fe9","type":"debug","z":"8be23377afc3327e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":320,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [23 March 2023 07:27 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/18 "2023-03-23T07:27:37Z")

</div>

It is not an error but a verbal comment from the client to fix the order of the objects.  
I will try your code. Thank You so much!

---

<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:** [23 March 2023 07:33 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/19 "2023-03-23T07:33:50Z")

</div>

Then tell the client that objects have no order and even if we put it in an order, it is not guaranteed. And their instruction is like asking you to use a sieve to carry water. A waste of time.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [23 March 2023 14:41 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/20 "2023-03-23T14:41:21Z")

</div>

The only way to do this would be to use a Map Object - plus some JavaScript tinkering. But as E1cid says, insisting on having an Objects properties in a particular order is daft.

or checkout [javascript - Changing the order of the Object keys.... - Stack Overflow](https://stackoverflow.com/questions/6959817/changing-the-order-of-the-object-keys)

[Next page](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791.md?page=2)
