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

标签 javascript arrays

我有以下数组:

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/

相关文章:

javascript - 使抽屉可调整大小的推荐方法?

javascript - 使用 angular.js 和 ui.utils 突出显示搜索结果

java - 对象类中的长度字段

javascript - 另一种获取数组 Javascript 整数的所有组合的方法

python - 加速循环以使用另一个数组中最接近的值填充数组

arrays - 如何在 PowerShell 中将 HashSet 转换为 ArrayList?

javascript - 标记.setMap(null);不起作用

javascript - 如何在不重置分数的情况下重置刽子手游戏?

c# - 就地 reshape c# 数组

javascript - 弹出页面中的自动对焦(甚至对焦)?