1

Sorry for my silly question.I have a list of users like this:

const users = [
    { name: "Jimmy", skills: ["Docker", "JavaScript", "Ruby"],age:20},
    { name: "Jimmy", skills: ["AWS", "Python"],age:15 },
    { name: "Frankie", skills: ["Azure", "JavaScript"],age:21},
    { name: "Liam", skills: ["Java", "JavaScript"],age:22},
    { name: "Fred", skills: ["JavaScript", "AWS"],age:34 },
    { name: "Sara", skills: ["PHP", "AWS"],age:23 },
    { name: "Matt", skills: [".Net", "PHP", "Docker"],age:25 },
     { name: "Matt", skills: [".Net", "PHP", "Docker"],age:10 },
];

I want to filter out Matt with the age of 25

const JavaScriptApplicants = users.filter(o => o.name !== 'Matt' && o.age !== 25);
console.log(JavaScriptApplicants)

But it filters out another one with age 10. I just want the 25 gone.

4
  • 3
    change && to || Commented Jul 8, 2020 at 6:38
  • OMG, that's correct. But what is the logic here? I think we need 2 conditions is true right? Commented Jul 8, 2020 at 6:40
  • 1
    When you want to keep an item your callback should return true. If o.name is Matt then o.name !== Matt will be false, making the entire expression evaluate to false, on the other hand, || will ensure that the name is both Matt and the age is 25 before evaluating to false Commented Jul 8, 2020 at 6:46
  • 1
    Thank you, guys. Hope the best. Commented Jul 8, 2020 at 8:45

2 Answers 2

6

You need OR instead of AND.

The reason is simple. If you take

 !(o.name === 'Matt' && o.age === 25)

read: if name equals matt and age equals 25 don't take it.

Then take De Morgan's laws

!(a && b) = !a || !b
!(a || b) = !a && !b

and convert the expression into one without NOT, you get

 !(o.name === 'Matt') || !(o.age === 25)
 o.name !== 'Matt' || o.age !== 25

const users = [
    { name: "Jimmy", skills: ["Docker", "JavaScript", "Ruby"],age:20},
    { name: "Jimmy", skills: ["AWS", "Python"],age:15 },
    { name: "Frankie", skills: ["Azure", "JavaScript"],age:21},
    { name: "Liam", skills: ["Java", "JavaScript"],age:22},
    { name: "Fred", skills: ["JavaScript", "AWS"],age:34 },
    { name: "Sara", skills: ["PHP", "AWS"],age:23 },
    { name: "Matt", skills: [".Net", "PHP", "Docker"],age:25 },
     { name: "Matt", skills: [".Net", "PHP", "Docker"],age:10 },
     { name: "Foo", skills: [".Net", "PHP", "Docker"],age:25 },
];


const JavaScriptApplicants = users.filter(o => o.name !== 'Matt' || o.age !== 25);
console.log(JavaScriptApplicants)
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

3 Comments

OMG, that's correct. But what is the logic here? I think we need 2 conditions is true right?
@kapilpandey, please see the edit. it works. please jhave a look to the expressions as well.
@kapilpandey why should this not work? if a other users has a age of 25 it wouldnt even reach the right side of the || operator if the name isnt Matt
2

const users = [
    { name: "Jimmy", skills: ["Docker", "JavaScript", "Ruby"],age:20},
    { name: "Jimmy", skills: ["AWS", "Python"],age:15 },
    { name: "Frankie", skills: ["Azure", "JavaScript"],age:21},
    { name: "Liam", skills: ["Java", "JavaScript"],age:22},
    { name: "Fred", skills: ["JavaScript", "AWS"],age:34 },
    { name: "Sara", skills: ["PHP", "AWS"],age:23 },
    { name: "Matt", skills: [".Net", "PHP", "Docker"],age:25 },
     { name: "Matt", skills: [".Net", "PHP", "Docker"],age:10 },
];


const JavaScriptApplicants = users.filter(o => !(o.name === "Matt" && o.age === 25));
console.log(JavaScriptApplicants)

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.