I am currently working on generating a pdfmake table with array keys in one column and the corresponding values in the next column.
Here is a snippet of the JSON from my firebase database:
{
"users" : {
"useruid" : {
"wills" : {
"assets" : {
"MY IPHONE XR " : [ "my Brother Fab", "my sister Kenny" ],
"bedside lamps" : [ "my cat Cuddly" ],
"bottle " : [ "My son Ayo" ]
},
"details" : {
"Daniel thompson" : "19A Silver Road "
},
"residual" : [ "my son Paul" ],
"testators" : [ "my cat cuddly" ]
}
}
}
}
The goal is to present the data in a table format like this:
PROPERTY | BENEFICIARY
MY IPHONE XR | MY BROTHER FAB, MY SISTER KENNY
BEDSIDE LAMPS | MY CAT CUDDLY
BOTTLE | MY SON AYO
Although I have attempted some code, it does not display the information neatly:
admin.database().ref('users').child('useruid').child('wills')
.child('assets').once('value').then(function(dataSnapshot1) {
const assets = [];
const beneficiaries = [];
dataSnapshot1.forEach((childSnapshot) => {
const childKey = childSnapshot.key;
const childData = childSnapshot.val();
assets.push( childKey );
beneficiaries.push( childData );
});
console.log(assets);
console.log(beneficiaries);
var docDefinition = {
content: [
'I DECLARE that my Executors or any Professional or person engaged in proving my Will and administering the estate may charge reasonable fees for their services ',
'\nI GIVE my property as follows:',
{text: 'Property and Beneficiaries ', style: 'subheader'},
{
style: 'tableExample',
table: {
widths: ['*',100, '*', 200],
body: [
['No.','Property', 'Beneficiary', 'Beneficiary Address'],
[`${assets}`, `${beneficiaries}`, {text: '{beneficiary address}'}]
]
}
},
Is there a way to iterate through the assets and beneficiaries arrays using a loop such as foreach
, which can output something similar to this:
rows= [];
rows.push([index, asset1, asset 1 beneficiaries])
rows.push([index +1, asset2, asset 2 beneficiaries])
and so forth?