亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號