```javascript
//看了一下问题,不知道我理解对了没,以下是我的思路:
//把 invoice 和 transaction 都当作一个交易表里面的记录,只是 type 不一样,就可以解决
const transactions = [
{ id: 1, type: 'invoice', name: 'JNL001',amount:100,balance:100 },
{ id: 2, type: 'invoice', name: 'JNL002',amount:-100,balance:0 },
{ id: 3, type: 'invoice', name: 'JNL003',amount:100,balance:100 },
{ id: 4, type: 'invoice', name: 'JNL004',amount:-100,balance:0 },
{ id: 5, type: 'invoice', name: 'JNL005',amount:130,balance:130 },
{ id: 6, type: 'transaction', name: 'INV001',amount:-100,balance:30 },
{ id: 7, type: 'invoice', name: 'JNL006',amount:100,balance:130 },
{ id: 8, type: 'invoice', name: 'JNL007',amount:-100,balance:30 },
{ id: 9, type: 'transaction', name: 'INV002',amount:-100,balance:0 }
];
function findRecordsBetween(name) {
const index = transactions.findIndex(transaction => {
if (
transaction.name === name){
if(transaction.type === 'transaction'){
return true;
}else {
throw new Error('必须输入一个 transaction 的 name');
}
}
});
if (index === -1) {
return []; // 如果未找到与该名称匹配的记录,返回空数组
}
const transactionIndex = transactions.slice(0, index ).findLastIndex(transaction => transaction.type === 'transaction');
if (transactionIndex === -1) {
return transactions.slice(0, index); // 如果未找到最近一次 transaction ,返回到开始所有记录
}
return transactions.slice(transactionIndex + 1, index);
}
// 用例:
console.log(findRecordsBetween('INV001'));
```