Reputation: 2011
I have the following array:
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 } ]
And 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;
}
}
This above function checks whether two objects in the array are the same, and differ only with respect to their summary value.
I have the following 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);
The above code removes all elements in array which are identical to first
. It then removes all elements which are identical to first but differ only with respect to the summary value from new_array
. I am expecting to get an empty array as a result of these filters.
However, when I print final_array
, I get the following:
[ 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 } ]
I have tested diffSummary
and it does return true when comparing first and the last element of array
. I am not sure why the last element of array
is not being filtered.
Any insights are appreciated.
Upvotes: 1
Views: 285
Reputation: 3629
It then removes all elements which are identical to first but differ only with respect to the summary value
This is not what your code is doing, res2 has
(o1.summary !== o2.summary)
which means if they differ then you want to include that object not exclude.
just change that to ===
and you will get empty output.
Rethink how filter works:
new_array.filter(o => diffSummary(o, first) === true)
// when an object of array will get a `true` value returned from diffSummary function then that element will be collected by filter into resulting array.
// so if res2 alongwith res and res1 is true then only this case will occur
// and your code is checking for o1.summary and o2.summary to be unequal.
// BUT, as per your expectation, you need to remove them when they are unequal. so the `true` condition need to be based on an equality comparison.
Other points (other than comments above)
1- Line
var new_array = array.filter(o => (JSON.stringify(o) !== JSON.stringify(first));
has syntax error, insert one more closing bracket )
at the end
2- Following logic
if (res && res1 && res2) {
return true;
} else {
return false;
}
can be simplified as a one-liner:
return res && res1 && res2;
Upvotes: 1