Having an array with specific data.
console.log(array)
[ Data {
sample_id: 'S001',
v_id: 21,
type: 'BD',
sf: 'ETV5',
ef: 'NTRK',
breakpoint1: '8669',
breakpoint2: '1728',
sge: 8,
ege: 19,
som: 207,
wgs: null,
inframe: 1,
platform: 'WR',
rnaconf: 'High',
reportable: 1,
targetable: 1,
path: 'C3',
evidence: null,
summary:
'Same as before',
comments: null },
Data {
sample_id: 'S001',
v_id: 21,
type: 'BD',
sf: 'ETV5',
ef: 'NTRK',
breakpoint1: '8669',
breakpoint2: '1728',
sge: 8,
ege: 19,
som: 207,
wgs: null,
inframe: 1,
platform: 'WR',
rnaconf: 'High',
reportable: 1,
targetable: 1,
path: 'C3',
evidence: null,
summary:
'Same as before',
comments: null },
Data {
sample_id: 'S001',
v_id: 21,
type: 'BD',
sf: 'ETV5',
ef: 'NTRK',
breakpoint1: '8669',
breakpoint2: '1728',
sge: 8,
ege: 19,
som: 207,
wgs: null,
inframe: 1,
platform: 'WR',
rnaconf: 'High',
reportable: 1,
targetable: 1,
path: 'C3',
evidence: null,
summary:
'An interesting development',
comments: null } ]
Analyze the following function:
function diffSummary(o1, o2) {
res = (o1.sample_id === o2.sample_id) && (o1.v_id === o2.v_id) && (o1.type === o2.type) && (o1.sf === o2.sf) && (o1.ef === o2.ef) && (o1.breakpoint1 === o2.breakpoint1) && (o1.breakpoint2 === o2.breakpoint2);
res1 = (o1.sge === o2.sge) && (o1.ege === o2.ege) && (o1.som === o2.som) && (o1.wgs === o2.wgs) && (o1.inframe === o2.inframe) && (o1.platform === o2.platform);
res2 = (o1.rnaconf === o2.rnaconf) && (o1.reportable === o2.reportable) && (o1.targetable === o2.targetable) && (o1.path === o2.path) && (o1.evidence === o2.evidence) && (o1.comments === o2.comments) && (o1.summary !== o2.summary);
if(res && res1 && res2) {
return true;
} else {
return false;
}
}
The purpose of this function is to compare two objects within the array and validate their equivalence except for the summary attribute.
Review the following section of code:
var first = array[0];
var new_array = array.filter(o => (JSON.stringify(o) !== JSON.stringify(first));
var final_array = new_array.filter(o => diffSummary(o, first) === true);
This code snippet eliminates identical elements compared to first
from the array. It then removes elements matching first but differing only in the summary attribute from new_array
. The expectation is to receive an empty array after these operations.
However, upon printing final_array
, unexpected results appear:
[ Data {
sample_id: 'S001',
v_id: 21,
type: 'BD',
sf: 'ETV5',
ef: 'NTRK',
breakpoint1: '8669',
breakpoint2: '1728',
sge: 8,
ege: 19,
som: 207,
wgs: null,
inframe: 1,
platform: 'WR',
rnaconf: 'High',
reportable: 1,
targetable: 1,
path: 'C3',
evidence: null,
summary:
'An interesting development',
comments: null } ]
The analysis of diffSummary
showed correctness when assessing first and the last element of array
. An issue arises as the last element remains unfiltered.
Any insights would be appreciated.