# Can't add blank object to array

**URL:** <https://discourse.nodered.org/t/cant-add-blank-object-to-array/66715>\
**Category:** General\
**Tags:** function-node\
**Created:** [21 August 2022 01:45 UTC](https://discourse.nodered.org/t/cant-add-blank-object-to-array/66715 "2022-08-21T01:45:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thefox07](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thefox07/32/66587_2.png) [@thefox07](https://discourse.nodered.org/u/thefox07)\
**Post date:** [21 August 2022 01:45 UTC](https://discourse.nodered.org/t/cant-add-blank-object-to-array/66715/1 "2022-08-21T01:45:59Z")

</div>

Hello,

The following code works fine in online JS compilers but not in the node-red function node. What am I missing?

I inject the following msg.payload:

```auto
{
    "domain": "homeassistant",
    "service": "turn_on",
    "target": {
        "entity_id": [
            "light.office_lights",
            "light.bedroom_lights"
        ]
    },
    "data": {
        "brightness_pct": 50
    }
}

```

Into this function node:

```auto
var lookup = [
    { "entity_id": "light.office_lights", "conditions": [{ "entity_id": "input_boolean.bedroom_occupied", "allowed_state": "off" }, { "entity_id": "binary_sensor.peloton_workout", "allowed_state": "off" }] }
];

for (var y in msg.payload.target.entity_id) {
    var lookupMatch = lookup.find(el => msg.payload.target.entity_id[y] === el.entity_id);

    msg.payload.target.entity_id[y] = [msg.payload.target.entity_id[y]];
    msg.payload.target.entity_id[y].condition = [];
   
    if(lookupMatch) { 
        for (var i in lookupMatch.conditions) {
            msg.payload.target.entity_id[y].condition.push(lookupMatch.conditions[i]);
        }
    }
    node.warn(msg.payload.target.entity_id[y]);
}
return msg;

```

I get this back, which is missing the matched condition objects:

```auto
{
	"domain": "homeassistant",
	"service": "turn_on",
	"target": {
		"entity_id": [
			[
				"light.office_lights"
			],
			[
				"light.bedroom_lights"
			]
		]
	},
	"data": {
		"brightness_pct": 50
	}
}

```

And I am actually expecting to see this:

```auto
{
	"domain": "homeassistant",
	"service": "turn_on",
	"target": {
		"entity_id": [
			[
				"light.office_lights",
				"condition": [ 
					{ "entity_id": "input_boolean.bedroom_occupied", "allowed_state": "off" },
					{ "entity_id": "binary_sensor.peloton_workout", "allowed_state": "off" } 
				] 
			],
			[
				"light.bedroom_lights"
			]
		]
	},
	"data": {
		"brightness_pct": 50
	}
}

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [21 August 2022 06:19 UTC](https://discourse.nodered.org/t/cant-add-blank-object-to-array/66715/2 "2022-08-21T06:19:50Z")

</div>

There are many ways to Rome, i would probably use something like this:

```auto
const lookup = [
    { "entity_id": "light.office_lights", "conditions": [{ "entity_id": "input_boolean.bedroom_occupied", "allowed_state": "off" }, { "entity_id": "binary_sensor.peloton_workout", "allowed_state": "off" }] }
];

const input = msg.payload
const result = input.target.entity_id.forEach((e,i) =>{
    let c = lookup.filter(x => x.entity_id == e)
    if(c.length>0){
        input.target.entity_id[i] = { entity_id: e, conditions:c.map(m => m.conditions ) }
    }
})

return msg;

```

result:

```auto
{
    "domain": "homeassistant",
    "service": "turn_on",
    "target": {
        "entity_id": [{
            "entity_id": "light.office_lights",
            "conditions": [
                [{
                    "entity_id": "input_boolean.bedroom_occupied",
                    "allowed_state": "off"
                }, {
                    "entity_id": "binary_sensor.peloton_workout",
                    "allowed_state": "off"
                }]
            ]
        }, "light.bedroom_lights"]
    },
    "data": {
        "brightness_pct": 50
    }
}

```

---

<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:** [20 October 2022 06:20 UTC](https://discourse.nodered.org/t/cant-add-blank-object-to-array/66715/3 "2022-10-20T06:20:07Z")

</div>

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