我有一个带有信用报告历史数组的loan对象,每个信用报告都有一个信用分数数组。我想知道最后一份信用报告的最新信用评分
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()“调用。
发布于 2019-04-10 23:26:12
我认为这应该是你的解决方案,但我没有数据集来测试它,所以我只是试图复制你的逻辑。
// 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
);
https://stackoverflow.com/questions/55603790
复制相似问题