练习如下:其中键是银行名称和值的对象是仅在该银行中拥有帐户的客户数量。
发问这个数组:
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 }
];
我现在有这个:
function banksFidelity() {
var map = accounts.reduce(function(map, account) {
var bankByid = account.bankId;
var clientByid = account.clientId;
map[bankByid] = map[clientByid] || 0 + clientByid
return map
}, {})
return map
}
当前结果是:
{1: 6, 2: 5, 3: 5}
期望的结果是:
{1: 4, 2: 4, 3: 4}
答案 0 :(得分:1)
您可以使用.foreach
方法遍历帐户对象数组,并通过检查客户是否已经拥有该银行的帐户来填充另一个对象。您剩下一个对象(clients
),它将每个客户与他使用的银行阵列相关联:
{
"1": [3, 2],
"2": [2, 3, 1],
"3": [1, 3, 2],
"4": [3],
"5": [3, 2, 1],
"6": [1]
}
使用另一个.foreach
循环填充另一个对象(exclusiveClients
),该对象将把银行与仅使用该银行以获得所需结果的客户数量相关联:
{
"1": 1,
"3": 1
}
代码如下:
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}]
let clients = {};
accounts.forEach(({bankId, clientId}) => {
if(clients[clientId] && !clients[clientId].includes(bankId)) {
clients[clientId].push(bankId);
} else if(!clients[clientId]) {
clients[clientId] = [bankId];
}
});
let exclusiveClients = {};
Object.entries(clients).forEach(([client, banks]) => {
if(banks.length == 1) {
const bank = banks[0];
exclusiveClients[bank] = (exclusiveClients[bank] + 1) || 1;
}
});
console.log(exclusiveClients)
答案 1 :(得分:1)
您可以reduce删除所有重复的数组,然后缩小该数组以形成最终对象。
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 }
]
let result = accounts
// Remove duplicates
.reduce((arr, itm) => {
let containsClient = !!arr.find(i => i.clientId == itm.clientId && i.bankId == itm.bankId)
return !containsClient ? arr.concat(itm) : arr
}, [])
// Get the counts after duplicates are removed
.reduce((obj, itm) => {
if (obj[itm.bankId]) obj[itm.bankId]++
else obj[itm.bankId] = 1
return obj
}, {})
console.log(result)
答案 2 :(得分:1)
变化:使用引用,因此当我们更新客户端一次时,我们可以从添加客户端/引用复制的任何地方检索信息
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 }
]
var clients = {}
var dupes = {}
var banks = {}
accounts.forEach(a=>{
if(dupes[a.clientId+'_'+a.bankId]){
return;
}
banks[a.bankId] = banks[a.bankId] || new Set;
if(clients[a.clientId]){
//this client has already a bank
//different than the current one
clients[a.clientId].multi = true;
}else{
clients[a.clientId]={}
}
banks[a.bankId].add(a.clientId)
dupes[a.clientId+'_'+a.bankId] = 1;
}, {})
var res = Object.keys(banks).reduce((acc,bId)=>{
acc[bId] = [...banks[bId]].filter(x=>!x.multi).length
return acc;
},{})
console.log('res ', res)