# Creating a variable sized object array

**URL:** <https://discourse.nodered.org/t/creating-a-variable-sized-object-array/41630>\
**Category:** General\
**Created:** [25 February 2021 16:55 UTC](https://discourse.nodered.org/t/creating-a-variable-sized-object-array/41630 "2021-02-25T16:55:04Z")\
**Posts on this page:** 4\
**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:** [25 February 2021 16:55 UTC](https://discourse.nodered.org/t/creating-a-variable-sized-object-array/41630/1 "2021-02-25T16:55:04Z")

</div>

I'm trying to create and store an array of objects with lenght `i` that I get from the length of my object array from an SQL query to my databse and initilize it with '0' to modify and use down the line. This is the code I'm using in a function node:

```auto
// Creates an array object to determine down the line if each individual recipe is met for the non-binary devices
for (i = 0;i < msg.myRecipes.length; i++){
    msg.conditionMet[i] = 0;
}

return msg;

```

But I get the following error. This seemed like the easiest way to do it to me but I can't get it to work...

`TypeError: Cannot set property '0' of undefined`

---

<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:** [25 February 2021 17:03 UTC](https://discourse.nodered.org/t/creating-a-variable-sized-object-array/41630/2 "2021-02-25T17:03:57Z")

</div>

before the for loop define conditional as an array.  
`msg.conditional = [];`

---

<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:** [25 February 2021 17:18 UTC](https://discourse.nodered.org/t/creating-a-variable-sized-object-array/41630/3 "2021-02-25T17:18:26Z")

</div>

There is a compact way of doing that in javascript now  
`msg.conditionMet = new Array( msg.myRecipes.length ).fill(0)`

---

<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:** [26 April 2021 17:18 UTC](https://discourse.nodered.org/t/creating-a-variable-sized-object-array/41630/4 "2021-04-26T17:18:45Z")

</div>

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