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

Hi!
I have a dataset in a excel sheet.

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?

1 Like

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 ...

//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

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

1 Like

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.

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

//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;
1 Like

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.

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")

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

If I change the for infor (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

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.

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 :frowning:

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

1 Like

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.

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

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;

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

1 Like

Fantastic, thanks a lot, this really solved the problem :smile: :+1:

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