javascript:包含每个银行客户的对象

时间:2018-07-03 00:07:39

标签: javascript aem-6

通过es6,练习包括显示对象,其中的键是银行的名称和值,并带有其客户的文本编号。

具有以下矩阵

 const clients = [
{ id: 1, taxNumber: '86620855', name: 'HECTOR ACUÑA BOLAÑOS'},
{ id: 2, taxNumber: '7317855K', name: 'JESUS RODRIGUEZ ALVAREZ'},
{ id: 3, taxNumber: '73826497', name: 'ANDRES NADAL MOLINA'},
{ id: 4, taxNumber: '88587715', name: 'SALVADOR ARNEDO MANRIQUEZ'},
{ id: 5, taxNumber: '94020190', name: 'VICTOR MANUEL ROJAS LUCAS'},
{ id: 6, taxNumber: '99804238', name: 'MOHAMED FERRE SAMPER' }
];
const accounts = [
{ clientId: 6, bankId: 1, balance: 15000 },
{ clientId: 1, bankId: 3, balance: 18000 },
{ clientId: 5, bankId: 3, balance: 135000 },
{ clientId: 2, bankId: 2, balance: 5600 },
{ clientId: 3, bankId: 1, balance: 23000 },
{ clientId: 5, bankId: 2, balance: 15000 },
{ clientId: 3, bankId: 3, balance: 45900 },
{ clientId: 2, bankId: 3, balance: 19000 },
{ clientId: 4, bankId: 3, balance: 51000 },
{ clientId: 5, bankId: 1, balance: 89000 },
{ clientId: 1, bankId: 2, balance: 1600 },
{ clientId: 5, bankId: 3, balance: 37500 },
{ clientId: 6, bankId: 1, balance: 19200 },
{ clientId: 2, bankId: 3, balance: 10000 },
{ clientId: 3, bankId: 2, balance: 5400 },
{ clientId: 3, bankId: 1, balance: 9000 },
{ clientId: 4, bankId: 3, balance: 13500 },
{ clientId: 2, bankId: 1, balance: 38200 },
{ clientId: 5, bankId: 2, balance: 17000 },
{ clientId: 1, bankId: 3, balance: 1000 },
{ clientId: 5, bankId: 2, balance: 600 },
{ clientId: 6, bankId: 1, balance: 16200 },
{ clientId: 2, bankId: 2, balance: 10000 }

]
const banks = [
{ id: 1, name: 'BankA' },
{ id: 2, name: 'BankB' },
{ id: 3, name: 'BankC' }
];

然后结果将是这样的:

0: {bank: 'BankA', clients: {'99804238', '94020190', '73826497'} } ,
1: {bank: 'BankB', clients: {'7317855K', '94020190', '86620855'} } ,
2: {bank: 'BankC', clients: {'73826497', '86620855', '88587715'} } 

基本上,此结果包含银行名称,该银行包含    客户值,其中包含所有客户的文本号    用那家银行。

最终结果将包含每个银行的客户总数,   仅参考图像。

function banksClientsTaxNumbers() {

var map = accounts.reduce(function(map, acc) {

    var bankByid = acc.bankId
    map[bankByid ] = {} 

    return map 
  }, {})

  console.log(map)

  var array = Object.keys(map).map(function(id) {

    var bank = banks.find(function(bank) {
        return bank.id == id;

    })

    var account = accounts.filter(function(account) {
        return account.bankId == id;

    })
    console.log(account)

    return {
      banco: bank.name,
      clients: account
    }
  });
  console.log(array)

}

这段代码的结果是:

image result

如您所见,列出了每个银行所包含的帐户,但是我们仍然需要联系客户以按文本号显示它们,请帮忙!

如果您达到本练习的逻辑,我先谢谢您。问候!

2 个答案:

答案 0 :(得分:2)

为减少时间复杂度,请将clients转换为由客户ID索引的taxNumber的对象,并为banks(由银行ID索引的索引)执行相同的操作。然后,您可以将reduce accounts放入由bankId索引的累加器中,将每个客户端添加到Set中(以进行重复数据删除),然后将Set转换为数组O(N)

const clients=[{id:1,taxNumber:'86620855',name:'HECTOR ACUÑA BOLAÑOS'},{id:2,taxNumber:'7317855K',name:'JESUS RODRIGUEZ ALVAREZ'},{id:3,taxNumber:'73826497',name:'ANDRES NADAL MOLINA'},{id:4,taxNumber:'88587715',name:'SALVADOR ARNEDO MANRIQUEZ'},{id:5,taxNumber:'94020190',name:'VICTOR MANUEL ROJAS LUCAS'},{id:6,taxNumber:'99804238',name:'MOHAMED FERRE SAMPER'}];const accounts=[{clientId:6,bankId:1,balance:15000},{clientId:1,bankId:3,balance:18000},{clientId:5,bankId:3,balance:135000},{clientId:2,bankId:2,balance:5600},{clientId:3,bankId:1,balance:23000},{clientId:5,bankId:2,balance:15000},{clientId:3,bankId:3,balance:45900},{clientId:2,bankId:3,balance:19000},{clientId:4,bankId:3,balance:51000},{clientId:5,bankId:1,balance:89000},{clientId:1,bankId:2,balance:1600},{clientId:5,bankId:3,balance:37500},{clientId:6,bankId:1,balance:19200},{clientId:2,bankId:3,balance:10000},{clientId:3,bankId:2,balance:5400},{clientId:3,bankId:1,balance:9000},{clientId:4,bankId:3,balance:13500},{clientId:2,bankId:1,balance:38200},{clientId:5,bankId:2,balance:17000},{clientId:1,bankId:3,balance:1000},{clientId:5,bankId:2,balance:600},{clientId:6,bankId:1,balance:16200},{clientId:2,bankId:2,balance:10000}]
const banks=[{id:1,name:'BankA'},{id:2,name:'BankB'},{id:3,name:'BankC'}]



const taxNumberByClientId = clients.reduce(
  (a, { id, taxNumber }) => Object.assign(a, { [id]: taxNumber }),
  {}
);
const banksByBankId = banks.reduce(
  (a, { id, name }) => Object.assign(a, { [id]: name }),
  {}
);

const taxNumbersByBank = accounts.reduce((a, { clientId, bankId }) => {
  const bankName = banksByBankId[bankId];
  const taxNumber = taxNumberByClientId[clientId];
  if (!a[bankName]) a[bankName] = new Set();
  a[bankName].add(taxNumber);
  return a;
}, {});
const results = Object.entries(taxNumbersByBank)
  .map(([bank, clientsSet]) => ({
    bank,
    clients: [...clientsSet]
  }));
console.log(results);

答案 1 :(得分:0)

可以进一步简化如下:

if (process.env.NODE_ENV === 'production') {
  app.use(express.static('client/build'));

  app.get('*', function(req, res) {
    res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
  });
}