我可以链接这两个“ .last” lodash调用吗?

时间:2019-04-10 02:16:43

标签: angular lodash

我有一个贷款对象,其中包含一组信用报告历史记录,每个信用报告都有一组信用评分。我想获得上次信用报告中的最后一个信用分数

let creditReportHistory = loanInfo.creditReportHistory;
let lastReport = creditReportHistory ? _.last(creditReportHistory) : null;
let lastScore = lastReport ? _.last(lastReport.creditScores) : null;
return (
  loanInfo.fico !== null &&          // has a score
  _.isArray(creditReportHistory) &&  // history is an array
  creditReportHistory.length > 0 &&  // at least one credit report
  lastScore === null                 // last report has a last score that is null
);

上面的代码基本上需要知道上一次报告的最后分数是否为空。其他条件不依赖于lodash的“ last()”调用。

1 个答案:

答案 0 :(得分:0)

我认为这应该是您的解决方案,但是我没有数据集可以对其进行测试,因此我只是在尝试复制您的逻辑。

// Your current code:
let creditReportHistory = loanInfo.creditReportHistory;
let lastReport = creditReportHistory ? _.last(creditReportHistory) : null;
let lastScore = lastReport ? _.last(lastReport.creditScores) : null;
return (
  loanInfo.fico !== null &&          // has a score
  _.isArray(creditReportHistory) &&  // history is an array
  creditReportHistory.length > 0 &&  // at least one credit report
  lastScore === null                 // last report has a last score that is null
);

// Updated:
const lastScore = _.chain(loanInfo.creditReportHistory)
  .last()
  .flatMap((lastReport) => lastReport.creditScores)
  .last()
  .value();

return (
  loanInfo.fico !== null
  && lastScore 
);