微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

循环遍历 PDFmake 表的两个单独数组

如何解决循环遍历 PDFmake 表的两个单独数组

我正在尝试创建一个 pdfmake 表,该表将数组键作为一列,将相应的数组值作为下一列。

以下是我的 firebase 数据库的 JSON:

{
  "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" ]
      }
    }
  }
}

目标是有一个类似于:

PROPERTY      |    BENEFICIARY

MY IPHONE XR  |    MY broTHER FAB,MY SISTER KENNY
bedSIDE LAMPS |    MY CAT cuddLY
BottLE        |    MY SON AYO

下面是我尝试过的代码,但它不是很整齐:

     admin.database().ref('users').child('useruid').child('wills')
    .child('assets').once('value').then(function(dataSnapshot1) {

        const assets = [];
        const benef = [];

        dataSnapshot1.forEach((childSnapshot) => {
                      const childKey = childSnapshot.key;
                      const childData = childSnapshot.val();
                      assets.push( childKey );  
                      benef.push( childData ); 
                    });
          console.log(assets);
          console.log(benef);

     

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}`,`${benef}`,{text: '{beneficiary address}'}]
                ]
            }
        },

有没有办法循环遍历资产数组和受益人数组,可能会使用 foreach 产生类似

rows= [];
rows.push([index,asset1,asset 1 beneficiaries])
rows.push([index +1,asset2,asset 2 beneficiaries])

继续?

解决方法

let rows = [
  ['Property','Beneficiary']
];
for (let i = 0; i < assets.length; i +=1) { // i suggest a for-loop since you need both arrays at a time 
  rows.push([assets[i],benef[i]]);
}

table: {
  body: rows,headerRows: 1
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。