나는 두 배열을 비교 한 후 삽입 및 업데이트로 그룹화하고 있습니다. 업데이트 배열에 일치하는 요소를 포함 끝날해야 id == Id
하지만, 어디 open_balance !- DocumentAmount
. 인서트 배열은 소자들을 포함한다 id != Id
. 나는 통해 모두를 실행하고 주목 toString()
, 다른 하나는 두 개의 서로 다른 SQL 데이터베이스에서 Integer 형식의 반면 String 형의 때문에 내 시나리오 하나의 방법.
자, 내가, 이제 첫 번째 그룹화 작업을 코드와 함께 -를 통해 findRecordsToUpdate()
이 두 경기 때문에 ID에 있지만 양, 두 가지 요소가 있어야합니다 - 기능, I는 업데이트가 제대로 일치하는 모든 요소를 얻을
recordsToUpdate: [
{ Id: '333', DocumentAmount: 20 },
{ Id: '444', DocumentAmount: 10 }
]
그러나에 의해 생산되는 삽입, findRecordsToInsert()
기능은 올바르지 않습니다. 내 데이터에 따르면 내에서 '555'의 ID로, 하나 개의 요소와 끝까지해야한다 recordsToUpdate
는 다른 배열에서 찾을 수 없습니다 ID가 유일한 요소이기 때문에, 배열 - 즉, 그것은 삽입하는 새로운 기록입니다. 하지만 그 대신 나는 내 현재 코드 13 개 요소 결국?
내가 여기에 놓친 거지 무슨 내가 내 recordsToInsert 배열에 채워 올바른 삽입을 얻기 위해 변경해야합니까?
const sourceArr = [
{ id: 111, open_balance: 10 },
{ id: 222, open_balance: 20 },
{ id: 333, open_balance: 10 },
{ id: 444, open_balance: 20 },
]
const targetArr = [
{ Id: '111', DocumentAmount: 10 },
{ Id: '222', DocumentAmount: 20 },
{ Id: '333', DocumentAmount: 20 },
{ Id: '444', DocumentAmount: 10 },
{ Id: '555', DocumentAmount: 10 },
]
function findRecordsToUpdate () {
if (sourceArr.length && targetArr.length) {
let recordsToUpdate = [];
for (let t of targetArr) {
for (let s of sourceArr) {
if ((t.Id.toString() == s.id.toString()) && (t.DocumentAmount != s.open_balance)) {
recordsToUpdate.push(t);
}
}
}
console.log('recordsToUpdate: ', recordsToUpdate);
return recordsToUpdate;
}
};
function findRecordsToInsert () {
if (sourceArr.length && targetArr.length) {
let recordsToInsert = [];
for (let t of targetArr) {
for (let s of sourceArr) {
if (t.Id.toString() != s.id.toString()) {
recordsToInsert.push(t);
}
}
}
console.log('recordsToInsert: ', recordsToInsert);
return recordsToInsert;
}
};
findRecordsToUpdate();
findRecordsToInsert();
그런데, 명확히하기 위해, 내 findRecordsToInsert()
함수는 반환해야 recordsToInsert
할 때 (555)가 첫 번째 배열에 존재하지 않는 두 번째 배열의 유일한 기록이다의 ID와 기록으로,이 같은 배열 :
recordsToInsert: [{ Id: '555', DocumentAmount: 10 }]
그래서, 명확하게하기 위해, 그것은 단지의 findRecordsToInsert()
현재 제대로 작동하지 않는 기능.
의 조합을 사용 Array#filter
하고 Array#find
에서 당신의 삽입 및 업데이트 배열을 얻을 targetArr
.
'use strict';
const sourceArr = [
{ id: 111, open_balance: 10 },
{ id: 222, open_balance: 20 },
{ id: 333, open_balance: 10 },
{ id: 444, open_balance: 20 },
];
const targetArr = [
{ Id: '111', DocumentAmount: 10 },
{ Id: '222', DocumentAmount: 20 },
{ Id: '333', DocumentAmount: 20 },
{ Id: '444', DocumentAmount: 10 },
{ Id: '555', DocumentAmount: 10 },
];
// Target records with a matching source (on ID) where the amount has changed.
// Update these.
const recordsToUpdate = targetArr
.filter(tar => sourceArr
.find(source => source.id === Number(tar.Id) && source.open_balance !== tar.DocumentAmount));
console.log(recordsToUpdate);
// Target records that have no matching source record (i.e. new records).
// Insert these.
const recordsToInsert = targetArr
.filter(tar => !sourceArr
.find(source => source.id === Number(tar.Id)));
console.log(recordsToInsert);
그냥 기록을 위해, 원래 코드 검색에서이 findRecordsToInsert
올바르지 않습니다. 이 문제를 해결하려면, 소스에서 각 대상을 찾기 위해 노력하고, 발견되지 않는 경우는, 삽입 배열에 대상을 추가 할 수 있습니다. 그러나 그것은 단지의 장황한 버전입니다 filter
- find
.
const sourceArr = [
{ id: 111, open_balance: 10 },
{ id: 222, open_balance: 20 },
{ id: 333, open_balance: 10 },
{ id: 444, open_balance: 20 },
]
const targetArr = [
{ Id: '111', DocumentAmount: 10 },
{ Id: '222', DocumentAmount: 20 },
{ Id: '333', DocumentAmount: 20 },
{ Id: '444', DocumentAmount: 10 },
{ Id: '555', DocumentAmount: 10 },
]
function findRecordsToInsert () {
if (sourceArr.length && targetArr.length) {
const recordsToInsert = [];
for (const t of targetArr) {
let found = false;
for (let i = 0; i < sourceArr.length && !found; ++i) {
if (t.Id.toString() === sourceArr[i].id.toString())
found = true;
}
if (!found)
recordsToInsert.push(t);
}
console.log('recordsToInsert: ', recordsToInsert);
return recordsToInsert;
}
};
findRecordsToInsert();