Home > OS >  Filter objects in array by any value within another array
Filter objects in array by any value within another array

Time:01-06

I have an array of objects, and an array of values that I need to filter the objects by. I can remove duplicates from the array, but trying to figure out how to filter the objects with ids that have a match (or using startsWith()) to filter objects with an id that starts with a given value.

In the end, the object with id "F11v6" should be removed from the resulting array.

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));

CodePudding user response:

You could have a look to the blalisted items an check if id is in the list.

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; }

A single loop approach

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(
        (s => ({ id }) =>
            !blacklistedComponents.some(v => id.includes(v)) &&
            !s.has(id) &&
            s.add(id)
        )(new Set)
    );

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

CodePudding user response:

You can filter those elements that match the ones from the disallowed list using !.some(). Then apply your filter to get rid of the duplicates, e.g.:

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)

CodePudding user response:

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)

CodePudding user response:

Not entirely sure if you just wanted to remove elements that start with a black listed item, or if ones that contain it anywhere.

Used composition to show how you could do either, and deconstruction to extract the relevant field as the parameter for each filter.

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)

  • Related