使用过滤器时如何设置条件?

阳台

我试图返回以下数组中破坏了关系的所有项目:

[
   { id: "1", option: { bound_id: "2" }},
   { id: "2", option: { bound_id: "12" }},
   { id: "12", option: { bound_id: "2" }}
]

如您所见bound_id,如果一个属性破坏了像这样的关系,则每个项目都使用属性链接在一起

[
   { id: "1", option: { bound_id: null }},
   { id: "2", option: { bound_id: "12" }},
   { id: "12", option: { bound_id: "2" }}
]

返回以下结果:

[
   { id: "2", option: { bound_id: "12" }}
   { id: "12", option: { bound_id: "2" }}
]

我正在使用以下代码:

const input = [
   { id: "1", option: { bound_id: null }},
   { id: "2", option: { bound_id: "12" }},
   { id: "12", option: { bound_id: "2" }}
];

const output = input.filter(a => a.option.bound_id);

console.log(output);

我要包括的是仅插入下一个项目的关系,一个示例更好:

[
   { id: "1", option: { bound_id: "2" }},
   { id: "2", option: { bound_id: "3" }},
   { id: "12", option: { bound_id: "2" }}
]

如您所见,具有id的项目2破坏了与id的关系,12并指向该ID3在集合中不存在的项目,在这种情况下,输出应为:

[
   { id: "1", option: { bound_id: "2" }},
   { id: "2", option: { bound_id: "3" }}
]

如何使用过滤器做到这一点?

一定的表现

在使用时.filter,您可能会添加到的Setid,并.filter根据是否bound_id在对象中包含被迭代的对象Set(如果没有,则将其添加到集合中;如果是,则使该项目未通过.filter测试)。如果索引为0,也请保留该项目,因为您总是想保留第一条记录:

const input = [
   { id: "1", option: { bound_id: "2" }},
   { id: "2", option: { bound_id: "3" }},
   { id: "12", option: { bound_id: "2" }}
];
const alreadyHave = new Set();
const filtered = input.filter(({ option }, index) => {
  const { bound_id } = option;
  if (!alreadyHave.has(bound_id) || index === 0) {
    alreadyHave.add(bound_id);
    return true;
  }
});
console.log(filtered);

如果根据评论,您实际上总是要删除第一项,则将条件更改为&& index !== 0

const input = [
   { id: "1", option: { bound_id: "2" }},
   { id: "2", option: { bound_id: "3" }},
   { id: "12", option: { bound_id: "2" }}
];
const alreadyHave = new Set();
const filtered = input.filter(({ option }, index) => {
  const { bound_id } = option;
  if (!alreadyHave.has(bound_id) && index !== 0) {
    alreadyHave.add(bound_id);
    return true;
  }
});
console.log(filtered);

或者,对于每个注释,如果第一项的逻辑应该相同,则index完全删除条件:

const input = [
   { id: "1", option: { bound_id: "2" }},
   { id: "2", option: { bound_id: "3" }},
   { id: "12", option: { bound_id: "2" }}
];
const alreadyHave = new Set();
const filtered = input.filter(({ option }, index) => {
  const { bound_id } = option;
  if (!alreadyHave.has(bound_id)) {
    alreadyHave.add(bound_id);
    return true;
  }
});
console.log(filtered);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用过滤器设置过滤数组?

如何使用过滤器为 ServletRequest 设置协议

如何在内部条件过滤器中使用过滤器?

全选时如何使用过滤器

使用条件格式应用过滤器框

设置响应头不使用过滤器-RESTeasy

页面加载时如何应用过滤器?

使用过滤器时CSS过渡模糊

使用过滤器时的重复绝对$索引

如何在使用过滤器和包含在柏树中时使用变量

在 Swift 中使用过滤器时如何解决元素位置

在Vue2中使用过滤器时如何重置子组件的值

当 RHS 使用过滤器时,pandas 向量化概念如何确保处理 samr 行?

当要比较的对象为空时如何使用过滤器?

使用过滤器时如何从两个数组返回2个值

在 JavaScript 中为 ArcGis 使用对象时如何为多个图层应用过滤器

使用过滤器时,如何从ResultExecutingContext获取操作的参数

使用过滤器查询*:*时,Solr默认情况下如何排序?

使用过滤器时,如何显示Jface TableViewer Refresh的进度?

在 Angular Material Table 中使用过滤器时如何排除 undefined 和 null?

使用过滤器功能时如何填充所有列?

当 URL 不变时,如何使用过滤器从网站上抓取数据?

使用过滤器时如何获取对象的某些属性

如何使用 dockerode 在 listNetworks 中使用过滤器?

使用过滤器过滤对象

如何在Angularjs中使用过滤器?

如何在grails中使用过滤器

Simpy - 如何异步使用过滤器存储

如何使用过滤器缩小类型?