Arrays -
const source = [1, 1, 1, 3, 4];
const target = [1, 2, 4, 5, 6];
Empty arrays -
const matches1 = [];
const matches2 = [];
const unmatches1 = [];
Loop -
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);
}
}
This is the output right now -
[ 1, 1, 1, 4 ]
at matches1
[ 3 ]
at unmatches1
[ 1, 1, 1, 4 ]
at matches2
The desired output -
[ 1, 4 ]
at matches1
[ 1, 1, 3 ]
at unmatches1
[ 1, 4 ]
at matches2
What I would like to add is when source
has a match with target
, the value will be deleted from the target
array, how can I achive that?