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

时间:2019-01-20 20:49:49

标签: javascript jquery

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

[
   { 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的关系,并指向集合中不存在的ID为3的项目输出应为:

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

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

2 个答案:

答案 0 :(得分:4)

使用.filter时,您可以根据是否包含要迭代的Set来添加id中的.filterbound_id中的Set .filter(如果没有,则将其添加到集合中;如果是,则使该项目不通过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);测试)。如果索引为0,也请保留该项目,因为您一直想保留第一条记录:

&& 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);条件:

transition: opacity 1s

答案 1 :(得分:1)

我的建议基于交叉检查:

const input1 = [
    { id: "1", option: { bound_id: null }}
];
const input2 = [
    { id: "1", option: { bound_id: "2" }},
    { id: "2", option: { bound_id: "3" }},
    { id: "12", option: { bound_id: "2" }}
];
const input3 = [
    { id: "1", option: { bound_id: null }},
    { id: "2", option: { bound_id: "12" }},
    { id: "12", option: { bound_id: "2" }}
];
const input4 = [
   { id: "1", option: { bound_id: "2" }},
   { id: "2", option: { bound_id: "12" }},
   { id: "12", option: { bound_id: "2" }}
];

var f = (arr) =>  {
    if (arr.length == 1)
        return arr;
    return arr.filter(function (e, i, a) {
        return (!!a[i+1] && e.option.bound_id == a[i+1].id) ||
                (!!a[i-1] && e.id == a[i-1].option.bound_id);
    })
};

console.log(f(input1));
console.log(f(input2));
console.log(f(input3));
console.log(f(input4));