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.
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...
Hopefully that is enough clues for you to solve this yourself?
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;
Thanks for your answer.
Still two things are unclear to me:
i need the for loop to iterate throughout the whole payload, if I chante data to payload, I have an error:
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?
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
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
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