1
我有以下数组:
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 } ]
以及以下功能:
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);
if(res && res1 && res2) {
return true;
} else {
return false;
}
}
上述函数检查数组中的两个对象是否相同,仅在它们的汇总值方面有所不同。
我有以下代码:
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);
上面的代码删除了数组中与 相同的所有元素first。然后删除所有与 first 相同但仅在汇总值方面不同的元素new_array。由于这些过滤器,我期望得到一个空数组。
我已经测试过diffSummary,在比较array. 我不确定为什么array没有过滤的最后一个元素。
任何见解表示赞赏。
郎朗坤
相关分类