gpt4 book ai didi

Javascript:过滤器函数不从数组中删除预期的元素

转载 作者:行者123 更新时间:2023-11-30 19:07:01 25 4
gpt4 key购买 nike

我有以下数组:

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);

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;
}
}

上述函数检查数组中的两个对象是否相同,是否仅在汇总值方面不同。

我有以下代码:

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 的汇总值方面不同的元素。由于这些过滤器,我希望得到一个空数组。

但是,当我打印 final_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:
'An interesting development',
comments: null } ]

我已经测试了 diffSummary,它在比较 array 的第一个和最后一个元素时返回 true。我不确定为什么 array 的最后一个元素没有被过滤。

欢迎任何见解。

最佳答案

It then removes all elements which are identical to first but differ only with respect to the summary value

这不是你的代码在做什么,res2 有

(o1.summary !== o2.summary)

这意味着如果它们不同,那么您希望包含该对象而不是排除该对象。

只需将其更改为 ===,您将得到空输出。

重新思考过滤器的工作原理:

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.

其他要点(以上评论除外)

1 行

 var new_array = array.filter(o => (JSON.stringify(o) !== JSON.stringify(first));

语法错误,在末尾再插入一个右括号)

2- 遵循逻辑

 if (res && res1 && res2) {
return true;
} else {
return false;
}

可以简化为一行:

 return res && res1 && res2;

关于Javascript:过滤器函数不从数组中删除预期的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58876533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com