javascript 按另一个数组中的任意值过滤数组中的对象

hlswsv35  于 2023-01-08  发布在  Java
关注(0)|答案(4)|浏览(168)

我有一个对象数组和一个需要用来过滤对象的值数组,我可以删除数组中的重复项,但是我试图找出如何过滤id匹配的对象(或者使用startsWith())来过滤id以给定值开头的对象。
最后,ID为“F11v6”的对象应从结果数组中删除。

let blacklistedComponents = ["F11", "U30"];
let components = [
  { id: "F11v6", type: "unknown" },
  { id: "U30v3", type: "unknown" },
  { id: "CH11", type: "unknown" },
  { id: "CT12", type: "true" },
  { id: "U03v5", type: "unknown" },
  { id: "CT12", type: "true" }
];
console.log(components.filter((v,i,a)=>a.findIndex(v2=>(v2.id===v.id))===i));
lymgl2op

lymgl2op1#

你可以看看blalisted项目,检查id是否在列表中。

const
    blacklistedComponents = ["F11", "U30"],
    components = [{ id: "F11v6", type: "unknown" }, { id: "U30v3", type: "unknown" }, { id: "CH11", type: "unknown" }, { id: "CT12", type: "true" }, { id: "U03v5", type: "unknown" }, { id: "CT12", type: "true" }],
    result = components
        .filter(({ id }) => !blacklistedComponents.some(v => id.includes(v)))
        .filter((s => ({ id }) => !s.has(id) && s.add(id))(new Set));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

单回路方法
一个二个一个一个

cedebl8k

cedebl8k2#

您可以使用!.some()过滤那些与不允许列表中的元素匹配的元素。然后应用您的过滤器去除重复的元素,例如:

const disallowed = ['F11', 'U30'];
const components = [
    {id: 'F11v6', type: 'unknown'},
    {id: 'U30v3', type: 'unknown'},
    {id: 'CH11', type: 'unknown'},
    {id: 'CT12', type: 'true'},
    {id: 'U03v5', type: 'unknown'},
    {id: 'CT12', type: 'true'}
];

const filtered = components
    .filter((v) => !disallowed.some(e => v.id.startsWith(e)))
    .filter((v,i,a)=>a.findIndex(v2=>(v2.id===v.id))===i);
console.log(filtered)
rjjhvcjd

rjjhvcjd3#

let blacklistedComponents = ["F11", "U30"]
let components = [
  { id: "F11v6", type: "unknown" },
  { id: "U30v3", type: "unknown" },
  { id: "CH11", type: "unknown" },
  { id: "CT12", type: "true" },
  { id: "U03v5", type: "unknown" },
  { id: "CT12", type: "true" }
]
const idAllowed = id => !blacklistedComponents.some(c=>id.startsWith(c))
const result = [...new Set(components.map(({id})=>id))] // unique ids
  .filter(idAllowed) // retain only non-blacklisted ids
  .map(id=>components.find(i=>id===i.id)) // correlate to components

console.log(result)
eeq64g8w

eeq64g8w4#

不完全确定您是否只想删除以黑名单项开头的元素,或者是否删除在任何地方包含该黑名单项的元素。
使用组合来展示如何执行其中一项操作,并使用解构来提取相关字段作为每个过滤器的参数。

const disallowed = ['F11', 'U30'];
const components = [
    {id: 'F11v6', type: 'unknown'},
    {id: 'U30v3', type: 'unknown'},
    {id: 'CH11', type: 'unknown'},
    {id: 'CT12', type: 'true'},
    {id: 'U03v5', type: 'unknown'},
    {id: 'CT12', type: 'true'}
];

let blackListFilterContains = ( {id} ) => !disallowed.some(testValue => id.includes(testValue));
let blackListFilterStartsWith = ( {id} ) => !disallowed.some(testValue => id.startsWith(testValue));
let uniqueFilter = (value, index, self) => self.indexOf(value) === index;

let result = components.filter(blackListFilterContains);
console.log(result)
result = result.filter(uniqueFilter);
console.log(result)

相关问题