# \[n00b Javascript\] creating an object from multiple objects using a custom function with for loop

**URL:** <https://discourse.nodered.org/t/n00b-javascript-creating-an-object-from-multiple-objects-using-a-custom-function-with-for-loop/42223>\
**Category:** General\
**Created:** [7 March 2021 21:26 UTC](https://discourse.nodered.org/t/n00b-javascript-creating-an-object-from-multiple-objects-using-a-custom-function-with-for-loop/42223 "2021-03-07T21:26:40Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![vongomben](https://avatars.discourse-cdn.com/v4/letter/v/a4c791/32.png) [@vongomben](https://discourse.nodered.org/u/vongomben)\
**Post date:** [7 March 2021 21:26 UTC](https://discourse.nodered.org/t/n00b-javascript-creating-an-object-from-multiple-objects-using-a-custom-function-with-for-loop/42223/1 "2021-03-07T21:26:41Z")

</div>

Hi!  
I have a dataset in a excel sheet.

 ![immagine](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/a/6ac612a1937a97fddfdc8a37bc4ef5e529d487be.png)

I know how to iterate in it, every 8 cells.  
I need a function that would create an object from each row.  
I am not able in writign that function.

payload[8].t would become payload.date  
payload[9].t would become payload.description  
payload[10].t would become payload.merc  
payload[11].t would become payload.amount  
payload[12].t would become payload.val  
payload[13.t would become payload.change  
payload[14].t would become payload.full\_amount  
payload[15].t would become payload.card

and reiterating, from 16, every 8 places.  
Thanks in advance for your help.

Any help?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [7 March 2021 21:34 UTC](https://discourse.nodered.org/t/n00b-javascript-creating-an-object-from-multiple-objects-using-a-custom-function-with-for-loop/42223/2 "2021-03-07T21:34:16Z")

</div>

Hi, while i dont quite understand what you have and where you need to get to, I can show you how to make an object from rows of data.

example ...

```javascript
//fake data...
var data = [
   [1, 2, 3], //row 1
   [4, 5, 6], //row 2
   [7, 8, 9], //row 3
]

var result = []; //somewhere to store new objects

for (let i = 0; i < data.length ; i++){
   let row = data[i];
   let newRow = {}; //new empty object
   newRow.col1 = row[0]; //add a prop called col1
   newRow.anotherObjectPropery = row[1]; //add a prop called anotherObjectPropery
   newRow.myProp = row[2]; //add a prop called myProp
   result.push(newRow); //add new object to result array
}

msg.payload = result;
return msg;

```

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

Hopefully that is enough clues for you to solve this yourself?

---

<div class="post-metadata">

**Author:** ![widobsono](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/widobsono/32/52336_2.png) [@widobsono](https://discourse.nodered.org/u/widobsono)\
**Post date:** [8 March 2021 09:15 UTC](https://discourse.nodered.org/t/n00b-javascript-creating-an-object-from-multiple-objects-using-a-custom-function-with-for-loop/42223/3 "2021-03-08T09:15:22Z")

</div>

Thank you, I was actually having the same conundrum.

I still don't get how to change the object name not value (ex: newRow.col1 ) so is it possible to change the **col1** or **anotherObjectProperty**?

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 March 2021 09:25 UTC](https://discourse.nodered.org/t/n00b-javascript-creating-an-object-from-multiple-objects-using-a-custom-function-with-for-loop/42223/4 "2021-03-08T09:25:00Z")

</div>

> [@widobsono](#):
>
> I still don't get how to change the object name not value (ex: newRow.col1 ) so is it possible to change the **col1** or **anotherObjectProperty**?

Look at the function code - I hand typed those names as an example. You can chose whatever property names you wish. e.g...

```auto
//fake data...
var data = [
   [1, 2, 3], //row 1
   [4, 5, 6], //row 2
   [7, 8, 9], //row 3
]

var result = []; //somewhere to store new objects

for (let i = 0; i < data.length ; i++){
   let row = data[i];
   let newRow = {}; //new empty object
   newRow.i_am_a_completely_different_prop_name = row[0]; //add a prop called i_am_a_completely_different_prop_name
   newRow.xxxxxx= row[1]; //add a prop called xxxxxx
   newRow.Frankenstein= row[2]; //add a prop called Frankenstein
   result.push(newRow); //add new object to result array
}

msg.payload = result;
return msg;

```

---

<div class="post-metadata">

**Author:** ![vongomben](https://avatars.discourse-cdn.com/v4/letter/v/a4c791/32.png) [@vongomben](https://discourse.nodered.org/u/vongomben)\
**Post date:** [8 March 2021 09:49 UTC](https://discourse.nodered.org/t/n00b-javascript-creating-an-object-from-multiple-objects-using-a-custom-function-with-for-loop/42223/5 "2021-03-08T09:49:32Z")

</div>

Thanks for your answer.  
Still two things are unclear to me:

1. i need the for loop to iterate throughout the whole payload, if I chante data to payload, I have an error:
2. you can see on the right of the picture the array of objects. I' have to pick the payload[0-8].t in order to create an object, so I guess I neet result to be an object?

I'm confused, I hope you'll be able to help me.

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 March 2021 09:58 UTC](https://discourse.nodered.org/t/n00b-javascript-creating-an-object-from-multiple-objects-using-a-custom-function-with-for-loop/42223/6 "2021-03-08T09:58:59Z")

</div>

use `msg.payload` to access the payload.

this becomes obvious if you put a debug node before the function (set the debug to show "complete message")

---

<div class="post-metadata">

**Author:** ![vongomben](https://avatars.discourse-cdn.com/v4/letter/v/a4c791/32.png) [@vongomben](https://discourse.nodered.org/u/vongomben)\
**Post date:** [8 March 2021 10:16 UTC](https://discourse.nodered.org/t/n00b-javascript-creating-an-object-from-multiple-objects-using-a-custom-function-with-for-loop/42223/7 "2021-03-08T10:16:54Z")

</div>

My apologies, but I still don't get it.

If I change the for in`for (let i = 0; i < msg.payload.length; i++){` I'm having a `TypeError: Cannot read property '0' of undefined` error.

If I leave it simply to `for (let i = 0; i < msg.payload; i++){` I' have an empty object.

If I input as limit of the loop "63" (which happen so be the lenght of this specific XLSX sheet, I have the error of before: `TypeError: Cannot read property '0' of undefined`

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [8 March 2021 11:17 UTC](https://discourse.nodered.org/t/n00b-javascript-creating-an-object-from-multiple-objects-using-a-custom-function-with-for-loop/42223/8 "2021-03-08T11:17:33Z")

</div>

maybe you mean `for (let i = 0; i < data.length; i++){` - as otherwise if msg.paylaod has length 3 or more then, as the error says, data[3] won't exist.

---

<div class="post-metadata">

**Author:** ![widobsono](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/widobsono/32/52336_2.png) [@widobsono](https://discourse.nodered.org/u/widobsono)\
**Post date:** [8 March 2021 13:53 UTC](https://discourse.nodered.org/t/n00b-javascript-creating-an-object-from-multiple-objects-using-a-custom-function-with-for-loop/42223/9 "2021-03-08T13:53:42Z")

</div>

Sorry my question was unclear. So I am wondering can we also assign the name of the object using a loop or from a list of name, so not just the value but also the property names.

newRow. **xxxxxx**

Let  
`name = ["Time", "Speed", "Mass"]`

If I put `newRow.name[0] = row[1]` to produce `newRow.Time = 3` it didn't work ☹

---

<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:** [8 March 2021 14:22 UTC](https://discourse.nodered.org/t/n00b-javascript-creating-an-object-from-multiple-objects-using-a-custom-function-with-for-loop/42223/10 "2021-03-08T14:22:22Z")

</div>

`newRow[name[i]] = row[i]`  
But make sure your name array has at least as many elements as the row does.

---

<div class="post-metadata">

**Author:** ![widobsono](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/widobsono/32/52336_2.png) [@widobsono](https://discourse.nodered.org/u/widobsono)\
**Post date:** [8 March 2021 14:43 UTC](https://discourse.nodered.org/t/n00b-javascript-creating-an-object-from-multiple-objects-using-a-custom-function-with-for-loop/42223/11 "2021-03-08T14:43:46Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/596aeec6c78a12d6cbb943dd1cafbce66808d533.png)

Thank you very much, this is a close solution. The only problem is that rather than `msg.payload.shutter = 12` it becomes `msg.payload[1].Shutter = 12`

I am still tweaking how to get rid of the index number.

---

<div class="post-metadata">

**Author:** ![widobsono](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/widobsono/32/52336_2.png) [@widobsono](https://discourse.nodered.org/u/widobsono)\
**Post date:** [8 March 2021 14:53 UTC](https://discourse.nodered.org/t/n00b-javascript-creating-an-object-from-multiple-objects-using-a-custom-function-with-for-loop/42223/12 "2021-03-08T14:53:51Z")

</div>

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

Yaay, (sort of) Solved it, I still hope to get rid of the index, but this is enough, I can address the object name.

```auto
var name = ["Data","Shutter","Speed"];
var data = [10, 12, 30000];

var result = []; //somewhere to store new objects
let newRow = {}; //new empty object

for (let i = 0; i < data.length ; i++){
   
    newRow[name[i]] = data[i];    

}

result.push(newRow); //add new object to result array
msg.payload = result;

return msg;

```

---

<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:** [8 March 2021 15:19 UTC](https://discourse.nodered.org/t/n00b-javascript-creating-an-object-from-multiple-objects-using-a-custom-function-with-for-loop/42223/13 "2021-03-08T15:19:27Z")

</div>

> [@widobsono](#):
>
> I still hope to get rid of the index,

It is there because you have created an empty array (result) then pushed newRow to it and then assigned the array to msg.payload, so it is not surprising that msg.payload is an array. Get rid of result and at the end use `msg.payload = newRow`

---

<div class="post-metadata">

**Author:** ![widobsono](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/widobsono/32/52336_2.png) [@widobsono](https://discourse.nodered.org/u/widobsono)\
**Post date:** [11 March 2021 05:03 UTC](https://discourse.nodered.org/t/n00b-javascript-creating-an-object-from-multiple-objects-using-a-custom-function-with-for-loop/42223/14 "2021-03-11T05:03:06Z")

</div>

Fantastic, thanks a lot, this really solved the problem 😄 👍

---

<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:** [10 May 2021 05:03 UTC](https://discourse.nodered.org/t/n00b-javascript-creating-an-object-from-multiple-objects-using-a-custom-function-with-for-loop/42223/15 "2021-05-10T05:03:54Z")

</div>

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