Search code examples
javascriptaem-6

javascript: object that contains the customers that belong to each bank


through es6, the exercise consists in showing objects in which the keys are the names of the banks and the values ​​in a disposition with the textnumber of their clients.

with the following matrices

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

then the result would be something like this:

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

Basically, this result contains the name of the bank that contains a client value, which contains all the text numbers of the clients that use that bank.

the final result would contain the total number of clients of each bank, only reference image.

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)

}

The result of this code is:

image result

As you can see, the accounts that each bank contains are listed, but we still have to reach the array of clients to show them by textnumber, help please!

If you achieve the logic of this exercise, I thank you in advance. Regards!


Solution

  • To reduce time complexity, turn the clients into an object of taxNumbers indexed by client ID, and do the same for the banks, indexed by bank ID. Then you can reduce the accounts into an accumulator indexed by bankId, add each client to a Set (to deduplicate), then transform the Set into an array, 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);