두 개의 객체 배열이 있습니다.
wallets = [
{ title: "Wallet 1", _id: "1", created: new Date("2020-07-14T00:00:00.000Z") },
{ title: "Wallet 2", _id: "2", created: new Date("2020-07-13T00:00:00.000Z") },
{ title: "Wallet 3", _id: "3", created: new Date("2020-07-19T00:00:00.000Z") }]
tcode = [
{ "createdOn": "2020-03-16T11:58:52.000Z", "type": "Credit", "_id": 2, "amount": 212.92, walletId: 1 },
{ "createdOn": "2020-01-01T15:21:58.000Z", "type": "Credit", "_id": 3, "amount": 624.22, walletId: 2 },
{ "createdOn": "2019-12-13T20:50:38.000Z", "type": "Credit", "_id": 4, "amount": -874.15, walletId: 3 },
{ "createdOn": "2020-05-25T22:37:20.000Z", "type": "Credit", "_id": 5, "amount": -502.68, walletId: 1 },
{ "createdOn": "2020-01-24T10:33:27.000Z", "type": "Debit", "_id": 6, "amount": 897.46, walletId: 2 },
{ "createdOn": "2019-09-24T00:45:15.000Z", "type": "Debit", "_id": 7, "amount": 981.36, walletId: 1 },
{ "createdOn": "2019-12-06T22:17:35.000Z", "type": "Credit", "_id": 8, "amount": -934.81, walletId: 2 },
{ "createdOn": "2019-09-06T03:49:31.000Z", "type": "Credit", "_id": 9, "amount": 818.77, walletId: 3 },
{ "createdOn": "2020-04-23T22:09:46.000Z", "type": "Credit", "_id": 10, "amount": 312.33, walletId: 3 },
{ "createdOn": "2020-07-18T09:15:12.000Z", "type": "Credit", "_id": 11, "amount": -739.77, walletId: 1 }
]
이제 객체를 필터링하는 지갑을 반복하고 다음을 사용하여 오름차순으로 정렬합니다.
for (let i in wallets){
temp = tcode.filter(data => data.walletId == wallets[i]._id).sort(((a, b) => { return new Date(b.createdOn) - new Date(a.createdOn) })).map(data=> ({
_id: data._id,
credit:data.type=='Credit'? data.amount:null,
debit:data.type=='Debit'? data.amount:null,
amount:data.amount,
createdOn:data.createdOn,
type:data.type,
walletId:data.walletId,
}))
}
이제 각 개체의 이전 금액을 모두 더한 새로 매핑 된 각 개체에 기초 잔액을 추가해야했습니다. Simple For 루프를 사용하여 활성화 할 수 있지만 축소 및 매핑을 사용하는 더 좋은 방법이 있습니까?
원하는 결과 :
[
{
_id: 6,
credit: null,
debit: 897.46,
amount: 897.46,
createdOn: '2020-01-24T10:33:27.000Z',
type: 'Debit',
walletId: 2,
openingBalance: 897.46
},
{
_id: 3,
credit: 624.22,
debit: null,
amount: 624.22,
createdOn: '2020-01-01T15:21:58.000Z',
type: 'Credit',
walletId: 2,
openingBalance: 1521.68
},
{
_id: 8,
credit: -934.81,
debit: null,
amount: -934.81,
createdOn: '2019-12-06T22:17:35.000Z',
type: 'Credit',
walletId: 2,
openingBalance: 586.87
}
]
각각 transactions
속성 이 있고 설명하는 배열을 갖는 지갑과 같은 객체의 배열을 생성하는 것이 좋습니다 .
그러나이 openBalance
합계는 반대 방향, 즉 이전 트랜잭션에서 가장 최근 트랜잭션으로 요약되어야합니다.
또한 정렬 작업을 한 번만 수행 한 다음 a Map
를 사용 하여 오른쪽 지갑 transaction
목록 의 데이터를 수집 합니다.
다음은 사용할 수있는 코드입니다. 보너스로 확장 지갑 개체 수준에서 현재 잔액을 등록합니다.
let wallets = [{ title: "Wallet 1", _id: "1", created: new Date("2020-07-14T00:00:00.000Z") },{ title: "Wallet 2", _id: "2", created: new Date("2020-07-13T00:00:00.000Z") },{ title: "Wallet 3", _id: "3", created: new Date("2020-07-19T00:00:00.000Z") }]
let tcode = [{ "createdOn": "2020-03-16T11:58:52.000Z", "type": "Credit", "_id": 2, "amount": 212.92, walletId: 1 },{ "createdOn": "2020-01-01T15:21:58.000Z", "type": "Credit", "_id": 3, "amount": 624.22, walletId: 2 },{ "createdOn": "2019-12-13T20:50:38.000Z", "type": "Credit", "_id": 4, "amount": -874.15, walletId: 3 },{ "createdOn": "2020-05-25T22:37:20.000Z", "type": "Credit", "_id": 5, "amount": -502.68, walletId: 1 },{ "createdOn": "2020-01-24T10:33:27.000Z", "type": "Debit", "_id": 6, "amount": 897.46, walletId: 2 },{ "createdOn": "2019-09-24T00:45:15.000Z", "type": "Debit", "_id": 7, "amount": 981.36, walletId: 1 },{ "createdOn": "2019-12-06T22:17:35.000Z", "type": "Credit", "_id": 8, "amount": -934.81, walletId: 2 },{ "createdOn": "2019-09-06T03:49:31.000Z", "type": "Credit", "_id": 9, "amount": 818.77, walletId: 3 },{ "createdOn": "2020-04-23T22:09:46.000Z", "type": "Credit", "_id": 10, "amount": 312.33, walletId: 3 },{ "createdOn": "2020-07-18T09:15:12.000Z", "type": "Credit", "_id": 11, "amount": -739.77, walletId: 1 }];
// Key the wallets by their id, and add a list of transactions for them
let map = new Map(wallets.map(wallet => [+wallet._id, ({...wallet, ...{ transactions: [], openBalance: 0 }})]));
// Get a sorted copy of the transactions
let sorted = [...tcode].sort((a, b) => a.createdOn.localeCompare(b.createdOn));
// Put each transaction in the corresponding wallet's list
for (let { createdOn, type, _id, amount, walletId } of sorted) {
let wallet = map.get(+walletId); // pick up the corresponding wallet
// Avoid floating point inaccuracies
wallet.openBalance = +(wallet.openBalance + amount).toFixed(2);
wallet.transactions.unshift({
_id,
credit: type == "Credit" ? amount : null,
debit: type == "Dedit" ? amount : null,
amount,
createdOn,
type,
walletId,
openBalance: wallet.openBalance
});
}
// Extract the result from the map
let richWallets = Array.from(map.values());
console.log(richWallets);
위의 내용은 소수를 추가 할 때 부동 소수점 정밀도 문제와 관련된 잠재적 인 문제도 해결합니다.
JavaScript에 적절한 10 진수 유형이 없기 때문에 실제로 금액을 센트로 저장하는 것이 좋습니다. 따라서 항상 정수입니다. 그런 다음 프레젠테이션 레이어에 적절한 통화로 프레젠테이션을 남겨두면 금액이 100으로 나뉩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다