为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Smart猫小萌 2022-01-01 20:40:11
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没有过滤的最后一个元素。任何见解表示赞赏。
查看完整描述

1 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

然后删除所有与 first 相同但仅在汇总值方面不同的元素


这不是你的代码在做什么, 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;


查看完整回答
反对 回复 2022-01-01
  • 1 回答
  • 0 关注
  • 155 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号