JavaScript - 如何使用另一个 JSON 中的 JSON ID 获取值

标签 javascript json ecmascript-6

练习如下:

It is required to obtain an Arrangement with the names of clients ordered from highest to lowest by the TOTAL sum of the balances.

使用这些 javascript 对象:

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

至此,我得到了每个客户的工资总和。这样:

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)  
 };

获得以下内容:

enter image description here

但我无法找到在另一个 json 中获取相应客户端名称的方法,并将其加入最终查询,到目前为止只显示 id 和总数,因为它们在同一个 json 中.

想要的结果

  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}

最佳答案

我会采取不同的方法,从客户端开始并创建一个代表最终输出的客户端对象。然后,只需对帐户调用 forEach 并将余额添加到客户端对象即可。

例如:

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

关于JavaScript - 如何使用另一个 JSON 中的 JSON ID 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51127404/

相关文章:

javascript - 找不到 sharejs 对象的方法 'attach'

json - 如何使用 jq 将 JSON 文档复制到另一个文档中

Python 和 fql : getting "Dami\u00e1n" instead of "Damián"

javascript - Traceur 运行时 : Super expression must either be null or a function, 未定义

javascript - 如何使用对象解构扩展运算符删除多个对象属性

java - Selenium Java - 通过 JavascriptExecutor 获取页面源

javascript - 使用从 AJAX 调用的内容到另一个 AJAX 调用中

javascript - 更好地处理 ajax 调用的附加层

javascript - JSON 解析抛出的意外 token 错误

javascript - es6 从静态函数访问类构造函数