I have a table that looks like this which I'm using JS to convert to a JS object:
|column-1|column-2|column-3|column-4|column-5|
|--------|--------|--------|--------|--------|
|My Name | Date |Message |John Doe|Phone #s|
|Jhn Doe2|Phone #s|
|Jhn Doe3|Phone #s|
|Jhn Doe4|Phone #s|
|Jhn Doe5|Phone #s|
This is my JS code:
var table = document.getElementById('table');
var jsonArr = [];
for(var i =0,row;row = table.rows[i];i++){
var colmn = row.cells;
var recipeints = [{
"phone_number": colmn[4].innerHTML,
"recipient_name": colmn[3].innerHTML
}];
var message = {
"message_by": colmn[0].innerHTML,
"message_date": new Date(),
"message_recipients": recipeints,
"message_text": colmn[2].innerHTML
};
console.log(message);
}
The issue I'm facing is that the console message outputs 5 different object arrays with the message data but I'd like only one object output with nested objects for columns 4 and 5.
This is an instance of the output:
{message_by: "My name", message_date: Mon Dec 18 2017 14:32:29, message_recipients: Array(1), message_text: "Message"}
{message_by: "", message_date: Mon Dec 18 2017 14:32:29, message_recipients: Array(1), message_text: ""}
{message_by: "", message_date: Mon Dec 18 2017 14:32:29, message_recipients: Array(1), message_text: ""}
{message_by: "", message_date: Mon Dec 18 2017 14:32:29, message_recipients: Array(1), message_text: ""}
{message_by: "", message_date: Mon Dec 18 2017 14:32:29, message_recipients: Array(1), message_text: ""}
And this is the output I'm aiming for:
{message_by: "My Name", message_date: Mon Dec 18 2017 14:32:29, message_recipients: Array(5), message_text: "test"}
message_by:"My Name"
message_date:Mon Dec 18 2017 14:32:29 {}
message_recipients:Array(5)
0:{phone_number: "0700100100", recipient_name: "John Doe"}
1:{phone_number: "0700100200", recipient_name: "John Doe 2"}
2:{phone_number: "0700100300", recipient_name: "John Doe 3"}
3:{phone_number: "0700100400", recipient_name: "John Doe 4"}
4:{phone_number: "0700100500", recipient_name: "John Doe 5"}
message_text:"sample"
That is, I'd like output only one object and merge the columns 4 and 5 to avoid null fields in multiple objects. How can I achieve this?
More Info
The code of the dynamically generated table is as shown:
<table id='table'>
<tr>
<td align='left' width='200'>My Name</td>
<td align='left' width='200'>2017-12-18 15:07:33</td>
<td align='left' width='200'>Sample Text</td>
<td align='left' width='200'>0700100100</td>
<td align='left' width='200'>John Doe</td>
</tr>
<tr>
<td align='left' width='200'></td>
<td align='left' width='200'></td>
<td align='left' width='200'></td>
<td align='left' width='200'>0700100200</td>
<td align='left' width='200'>John Doe 2</td>
</tr>
<tr>
<td align='left' width='200'></td>
<td align='left' width='200'></td>
<td align='left' width='200'></td>
<td align='left' width='200'>0700100300</td>
<td align='left' width='200'>John Doe 3</td>
</tr>
<tr>
<td align='left' width='200'></td>
<td align='left' width='200'></td>
<td align='left' width='200'></td>
<td align='left' width='200'>0700100400</td>
<td align='left' width='200'>John Doe 4</td>
</tr>
<tr>
<td align='left' width='200'></td>
<td align='left' width='200'></td>
<td align='left' width='200'></td>
<td align='left' width='200'>0700100500</td>
<td align='left' width='200'>John Doe 5</td>
</tr>
</table>