1

I have an array of objects lookups and an array filters. Now I want to filter the array from an array of objects using the object attribute name.

I used filter and some but I could not get the expected result.

EXPECTED RESULT:

[{id: 3, name: "Linkedin"}]

let lookups = [
{id: 1, name: "Twitter"},
{id: 2, name: "Facebook"},
{id: 3, name: "Linkedin"}
]

let filters = ["Facebook", "Twitter"]

const filtered = lookups.filter(lookup => filters.some(filter => filter.toLowerCase() != lookup.name.toLowerCase()));

console.log(filtered)

1 Answer 1

3

Your code gives those elements in result for which some of the values in filters is not equal to element's name. So for each element some of the name is not equal.

In other words you are using != with || which will always return true.

let a = "anything";
console.log(a !== "thing1" || a !== "thing2")

You need to use every() instead of some().

let lookups = [
{id: 1, name: "Twitter"},
{id: 2, name: "Facebook"},
{id: 3, name: "Linkedin"}
]

let filters = ["Facebook", "Twitter"]

const filtered = lookups.filter(lookup => filters.every(filter => filter.toLowerCase() !== lookup.name.toLowerCase()));

console.log(filtered)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.