JavaScript - how to get value by using a JSON ID that is in another JSON

Multi tool use
JavaScript - how to get value by using a JSON ID that is in another JSON
The exercise is as follows:
It is required to obtain an Arrangement with the names of clients ordered from highest to lowest by the TOTAL sum of the balances.
with these javascript objects:
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' }
];
and:
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 }
]
So far, I get the total sum of the salaries of each client. in this way:
function sortClientsTotalBalances() {
var map = accounts.reduce(function(map, account) {
var clientId = account.clientId
var balance = +account.balance
map[clientId] = (map[clientId] || 0) + balance
return map
}, {})
console.log(map)
var obj = clients;
var array = Object.keys(map).map(function(name) {
return {
fullName: name,
totalbalance: map[name]
}
})
console.log(array)
};
obtaining the following:
but I cannot find the way to get the corresponding name of the client that is in the other json, and join it to the final query, which so far only shows the id with the total, since they are in the same json.
desired result
0: { name: 'HECTOR ACUÑA BOLAÑOS', totalbalance: 8340 },
1: { name: 'JESUS RODRIGUEZ ALVAREZ', totalbalance: 5000},
2: { name: 'ANDRES NADAL MOLINA', totalbalance: 7500 },
3: { name: 'SALVADOR ARNEDO MANRIQUEZ', totalbalance: 6500},
4: { name: 'VICTOR MANUEL ROJAS LUCAS', totalbalance: 9300},
5: { name: 'MOHAMED FERRE SAMPER' , totalbalance: 8500}
they are not "JSON's" ... they are javascript objects ... which makes it easier of course
– Jaromanda X
Jul 1 at 22:23
I'd first change clients to be in the form of
{ 1: {taxNumber: '86620855', name: 'HECTOR ACUÑA BOLAÑOS'}, 2: {taxNumber: '7317855K', name: 'JESUS RODRIGUEZ ALVAREZ'}}
- this is easy to do, and will make the next step simple as well– Jaromanda X
Jul 1 at 22:25
{ 1: {taxNumber: '86620855', name: 'HECTOR ACUÑA BOLAÑOS'}, 2: {taxNumber: '7317855K', name: 'JESUS RODRIGUEZ ALVAREZ'}}
It is required to obtain....
can you show the exact format of the required output, rather than a vague description of it– Jaromanda X
Jul 1 at 22:26
It is required to obtain....
Right! This is the desired result!
– user7672972
Jul 1 at 22:28
4 Answers
4
I would take a different approach by starting with the clients and creating a client object that represents your final output. Then it's a simple matter of calling forEach
on the accounts and add the balance to the client object.
forEach
For example:
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 }]
// make client object that looks like final result
const client_obj = clients.reduce((a,c) => {
a[c.id] = {name: c.name, totalbalance: 0}
return a
}, {})
// just add balances to appropriate value of that object
accounts.forEach(item => client_obj[item.clientId].totalbalance += item.balance)
// sort object objvalues
console.log(Object.values(client_obj).sort((a,b) => a.totalbalance - b.totalbalance))
Pop this chunk on the end of your function?
for (var i=0;i<array.length();i++){
//find the corresponding name of the client
for (var j=0;j<clients.length();j++){
if (array[i].fullName==clients[j].id){
array[i].name=clients[j].name;
}
}
//Rearrange fullName and ID
array[i].id=parseInt(array[i].fullName);
delete array[i].fullName;
}
When you call Object.keys
on the object map
you get an array of IDs because that's how you construct it (the object map
) with Array#reduce
. So when you call Array#map
on that array of IDs, the value that get passed to the callback is the ID not the name. Slightly change Array#map
code to fetch the name from the clients
array like so:
Object.keys
map
map
Array#reduce
Array#map
Array#map
clients
var array = Object.keys(map).map(function(id) {
var client = clients.find(function(client) { // find the client from the array clients
return client.id == id; // ... whose id is the same as the current id
});
return {
fullName: client.name, // use that client name
totalbalance: map[id] // access the balance using id
};
});
Example:
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 } ];
var map = accounts.reduce(function(map, account) {
var clientId = account.clientId;
var balance = +account.balance;
map[clientId] = (map[clientId] || 0) + balance;
return map;
}, {});
var array = Object.keys(map).map(function(id) {
var client = clients.find(function(client) { // find the client from the array clients
return client.id == id; // ... whose id is the same as the current id
});
return {
fullName: client.name, // use that client name
totalbalance: map[id] // access the balance using id
};
});
console.log(array);
Another approach would be to turn the clients
array into an object where the keys are the IDs and the values are the names so there won't be a need to use Array#find
:
clients
Array#find
var clientsById = clients.reduce(function(obj, client) {
obj[client.id] = client.name;
return obj;
}, {});
var array = Object.keys(map).map(function(id) {
return {
fullName: clientsById[id], // use id to get the name from the object clientsById
totalbalance: map[id] // access the balance using id
};
});
Example:
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 } ];
var map = accounts.reduce(function(map, account) {
var clientId = account.clientId;
var balance = +account.balance;
map[clientId] = (map[clientId] || 0) + balance;
return map;
}, {});
var clientsById = clients.reduce(function(obj, client) {
obj[client.id] = client.name;
return obj;
}, {});
var array = Object.keys(map).map(function(id) {
return {
fullName: clientsById[id], // use id to get the name from the object clientsById
totalbalance: map[id] // access the balance using id
};
});
console.log(array);
Note: You still need to sort the resulting array using Array#sort
. I won't spoil that for you.
Array#sort
Absolutely correct!, I just needed a guide and you have been successful! thank you very much! :D
– user7672972
Jul 1 at 22:44
@DemaroCreate You're welcome! I've fixed some typos and added working snippetts to the answer. Check them out!
– ibrahim mahrir
Jul 1 at 22:47
I would map to wanted result like this :
function sortClientsTotalBalances(clients, accounts) {
// Every client are mapped to wanted data result.
let results = clients.map((client) => {
// Here I get client total balance by using reduce
let clientBalance = accounts.reduce((acc, account) => {
if (account.clientId === client.id ) {
acc += account.balance;
}
return acc;
}, 0);
// Here I create wanted result object;
let result = {
id: client.id,
name: client.name,
totalBalance: clientBalance
};
return result;
});
// Return sorted results
return results.sort((a, b) => a.totalBalance < b.totalBalance );
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Can you post your desired output?
– CertainPerformance
Jul 1 at 22:22