-1

通过 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)

}

这段代码的结果是:

图像结果

如您所见,每个银行包含的帐户都已列出,但我们仍然需要通过 textnumber 到达客户数组以显示它们,请帮助!

如果你实现了这个练习的逻辑,我提前谢谢你。问候!

4

2 回答 2

2

为降低时间复杂度,将转为由客户 ID 索引clients的 s 对象,对由银行 ID 索引的 , 执行相同操作。然后你可以进入一个由 索引的累加器,将每个客户端添加到一个(去重复),然后将 转换为一个数组,:taxNumberbanksreduceaccountsbankIdSetSetO(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);

于 2018-07-03T00:28:28.947 回答
0

可以进一步简化如下:

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' }
];

function banksClientsTaxNumbers() {
  return banks.reduce((obj, bank) => {
    return {
      ...obj,
      [bank.name]: accounts
        .filter(account => account.bankId === bank.id)
        .map(account => account.clientId)
        .filter((value, index, array) => array.indexOf(value) === index)
        .map(clientId => clients.find(client => client.id === clientId))
        .sort(function (a, b) { return a.name.toLowerCase().localeCompare(b.name.toLowerCase()); })
        .map(client => client.taxNumber)
    }
  }, {})
}



console.log(banksClientsTaxNumbers());

于 2020-06-07T08:16:31.427 回答