# Variable Sized Zero Filled Array Creates 'Undefined' Object

**URL:** <https://discourse.nodered.org/t/variable-sized-zero-filled-array-creates-undefined-object/45493>\
**Category:** General\
**Created:** [11 May 2021 13:06 UTC](https://discourse.nodered.org/t/variable-sized-zero-filled-array-creates-undefined-object/45493 "2021-05-11T13:06:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![quentinP](https://avatars.discourse-cdn.com/v4/letter/q/ba9def/32.png) [@quentinP](https://discourse.nodered.org/u/quentinP)\
**Post date:** [11 May 2021 13:06 UTC](https://discourse.nodered.org/t/variable-sized-zero-filled-array-creates-undefined-object/45493/1 "2021-05-11T13:06:37Z")

</div>

I'm trying to create an array of a certain length filled with 0. I used this code to do it:

```auto
// Create the "conditionMet" object to determine if the recipes conditions have been met by the triggers
msg.conditionMet = new Array(msg.myRecipes.length).fill(0)

```

The array seems to initialize correctly as seen through the debug node:

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

Then when I try to modify the values through a switch statement (part of it here):

```auto
for (i = 0; i < msg.myRecipes.length; i++){
    // Gets the current trigger condition to pass in the switch statement
    //var triggerCondition = msg.triggerConditionArr[i];
    var triggerCondition = msg.myRecipes.r_Trigger[i];
    
    switch(msg.triggerCondition){
        // Half Ranges
        case "percentage-S-50":
            if (potentiometerVal <= 50){
                msg.conditionMet[i] = 1;
            }
            break;
        case "percentage-G-51":
            if (potentiometerVal >= 51){
                msg.conditionMet[i] = 1;
            }
            break;
        // Increments of 10 ranges
        case "percentage-B-0-9":
            if (potentiometerVal >= 0 && potentiometerVal <= 9){
                msg.conditionMet[i] = 1;
            }
            break;
        (.... switch statement continues here)

```

I get the following error:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/b/5bb3633b55c3e9b5406f92b04c2761aebc1c1188.png)

I'm not sure how else to create the array...

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [11 May 2021 13:17 UTC](https://discourse.nodered.org/t/variable-sized-zero-filled-array-creates-undefined-object/45493/2 "2021-05-11T13:17:11Z")

</div>

That code is correct, so the issue will be somewhere else in your flow.

The first debug screenshot you have shared is showing is `msg.triggerConfitionArr` - which is a different property to the one in the initial code - `msg.conditionMet`. So that doesn't tell us anything about the original array.

In your bigger piece of code, you configure the `for` loop to iteration from `0` to `msg.myReceipes.length`. But in the next line you access `msg.myRecipes.r_trigger[i]`.

Without knowing the structure of your message, I can't say what it should be - but I can tell you what you have currently is wrong.

---

<div class="post-metadata">

**Author:** ![quentinP](https://avatars.discourse-cdn.com/v4/letter/q/ba9def/32.png) [@quentinP](https://discourse.nodered.org/u/quentinP)\
**Post date:** [11 May 2021 13:29 UTC](https://discourse.nodered.org/t/variable-sized-zero-filled-array-creates-undefined-object/45493/3 "2021-05-11T13:29:09Z")

</div>

You're right, the arrays were actually created correctly! I editted the question accordingly. I also understand about the line:

```auto
var triggerCondition = msg.myRecipes.r_Trigger[i];

```

It was structured as follows before and it gave the same error message (the first line is outside the for loop)

```auto
// Extract the trigger conditions in an array 
msg.triggerConditionArr = msg.myRecipes.r_Trigger;

var triggerCondition = msg.triggerConditionArr[i];

```

Basically I'm trying to extract the different recipe triggers to compare in the switch statement in each itteration of the for loop:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/9/29954414eae036caf8a0aa8f6a89999d6e31121c.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [11 May 2021 13:32 UTC](https://discourse.nodered.org/t/variable-sized-zero-filled-array-creates-undefined-object/45493/4 "2021-05-11T13:32:32Z")

</div>

In which case, I think you need to change the opening lines of the loop as follows (I've made it more verbose than it needs to be, but hopefully that helps make it clearer)

```auto
for (i = 0; i < msg.myRecipes.length; i++){
    // Get the recipe
    var recipe = msg.myRecipes[i];
    // Get the r_trigger for this recipe
    var triggerCondition = recipe.r_Trigger;
    // Use that value in the switch:
    switch(triggerCondition){

```

---

<div class="post-metadata">

**Author:** ![quentinP](https://avatars.discourse-cdn.com/v4/letter/q/ba9def/32.png) [@quentinP](https://discourse.nodered.org/u/quentinP)\
**Post date:** [11 May 2021 14:27 UTC](https://discourse.nodered.org/t/variable-sized-zero-filled-array-creates-undefined-object/45493/5 "2021-05-11T14:27:48Z")

</div>

Yeah, I see what the problem was. Thanks a lot !!

---

<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:** [25 May 2021 14:28 UTC](https://discourse.nodered.org/t/variable-sized-zero-filled-array-creates-undefined-object/45493/6 "2021-05-25T14:28:28Z")

</div>

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