Comparing Arrays -
const source = [1, 1, 1, 3, 4];
const target = [1, 2, 4, 5, 6];
Initializing Arrays -
const matches1 = [];
const matches2 = [];
const unmatches1 = [];
Array Matching Process -
for (const line of source) {
const line2Match = target.find((line2) => {
const isMatch = line === line2;
return isMatch;
});
if (line2Match) {
matches1.push(
line
);
matches2.push(line2Match);
} else {
unmatches1.push(line);
}
}
Current Output -
[ 1, 1, 1, 4 ]
found in matches1
[ 3 ]
added to unmatches1
[ 1, 1, 1, 4 ]
at matches2
Desired Output -
[ 1, 4 ]
should be in matches1
[ 1, 1, 3 ]
should be in unmatches1
[ 1, 4 ]
should be in matches2
Additional Functionality Request -
If a match occurs between source
and target
, the matched value should be removed from the target
array. How can this be achieved?