Zaif Warm
Zaif Warm

Reputation: 495

Javascript filter not working for multiple condition

I am trying to filter array of objects on the basis of some conditions. If I apply single condition it works but if I applied some more condition it doesn't worked!!. I don't understand why its not working for multiple conditions.Below is my code.

const removeItemIfConditationMatch = () => {
  let filter = [];
  filter = arr.filter(
    item => item.id !== myObj.id && 
    item.level !== myObj.level && 
    item.technology !== myObj.technology && 
    item.type !== myObj.type)
  console.log(filter)
}

let myObj = {
  "questions": [],
  "technology": "Can_Js",
  "type": "NON_CODE",
  "level": "EASY",
  "id": 0
}
let arr = [{
    "questions": [],
    "technology": "Can_Js",
    "type": "NON_CODE",
    "level": "EASY",
    "id": 0
  },
  {
    "questions": [],
    "technology": "Network_Simulator",
    "type": "NON_CODE",
    "level": "EASY",
    "id": 1
  },
  {
    "questions": [],
    "technology": "Java",
    "type": "NON_CODE",
    "level": "HIGH",
    "id": 0
  },
  {
    "questions": [],
    "technology": "Java",
    "type": "CODE",
    "level": "HIGH",
    "id": 1
  },
  {
    "questions": [],
    "technology": "React Js",
    "type": "NON_CODE",
    "level": "MEDIUM",
    "id": 1
  }
]
removeItemIfConditationMatch()

In the above code if I filter on single condition it works but if you run above code it won't work. Anyone have any idea why Its not working where I am doing wrong. Any help will be appreciated.

After applying my multi filter criteria I am expecting below result.

[
    {
        "questions": [],
        "technology": "Network_Simulator",
        "type": "NON_CODE",
        "level": "EASY",
        "id": 1
    },
    {
        "questions": [],
        "technology": "Java",
        "type": "NON_CODE",
        "level": "HIGH",
        "id": 0
    },
    {
        "questions": [],
        "technology": "Java",
        "type": "CODE",
        "level": "HIGH",
        "id": 1
    },
    {
        "questions": [],
        "technology": "React Js",
        "type": "NON_CODE",
        "level": "MEDIUM",
        "id": 1
    }
]

In short I am expecting if my condition match then remove that Object from array list.

Upvotes: 1

Views: 1198

Answers (4)

agershun
agershun

Reputation: 4107

Try to replace the condition to:

filter = arr.filter(item => !(item.id == myObj.id &&
                    item.level == myObj.level && 
                    item.technology == myObj.technology && 
                    item.type == myObj.type))

In your code the original filter removes all objects where ANY field equals to the value in myObj object. In suggested code filter function removes objects where ALL fields are not equal to the fields in myObj.

The problem is here: if you want to remove value from an array with the condition:

   item.id !== myObj.id && item.level !== myObj.level ...

if id fields are not equal then JS stops to verify the condition and drops this element from the result array.

Upvotes: 4

Prebiusta
Prebiusta

Reputation: 479

Try to use this filter instead

arr.filter((item) => JSON.stringify(item) !== JSON.stringify(myObj));

Remember that in your solution you specified multiple conditions, which usually have unexpected behavior in JavaScript. Logic is inverted, therefore you would have to use || instead of &&

Upvotes: 0

Long Vu
Long Vu

Reputation: 180

const removeItemIfConditationMatch = () => {
  let filter = [];
  let myObj = {
    questions: [],
    technology: "Can_Js",
    type: "NON_CODE",
    level: "EASY",
    id: 0,
  };
  let arr = [
    {
      questions: [],
      technology: "Can_Js",
      type: "NON_CODE",
      level: "EASY",
      id: 0,
    },
    {
      questions: [],
      technology: "Network_Simulator",
      type: "NON_CODE",
      level: "EASY",
      id: 1,
    },
    {
      questions: [],
      technology: "Java",
      type: "NON_CODE",
      level: "HIGH",
      id: 0,
    },
    {
      questions: [],
      technology: "Java",
      type: "CODE",
      level: "HIGH",
      id: 1,
    },
    {
      questions: [],
      technology: "React Js",
      type: "NON_CODE",
      level: "MEDIUM",
      id: 1,
    },
  ];
  filter = arr.filter(
    (item) =>
      item.id !== myObj.id ||
      item.level !== myObj.level ||
      item.technology !== myObj.technology ||
      item.type !== myObj.type
  );
  console.log(filter);
};

removeItemIfConditationMatch();

Upvotes: 1

Steve
Steve

Reputation: 1953

const removeItemIfConditationMatch = () => {
  let filter = [];
  filter = arr.filter(
    item => item.id !== myObj.id || 
    item.level !== myObj.level || 
    item.technology !== myObj.technology || 
    item.type !== myObj.type)
  console.log(filter)
}

let myObj = {
  "questions": [],
  "technology": "Can_Js",
  "type": "NON_CODE",
  "level": "EASY",
  "id": 0
}
let arr = [{
    "questions": [],
    "technology": "Can_Js",
    "type": "NON_CODE",
    "level": "EASY",
    "id": 0
  },
  {
    "questions": [],
    "technology": "Network_Simulator",
    "type": "NON_CODE",
    "level": "EASY",
    "id": 1
  },
  {
    "questions": [],
    "technology": "Java",
    "type": "NON_CODE",
    "level": "HIGH",
    "id": 0
  },
  {
    "questions": [],
    "technology": "Java",
    "type": "CODE",
    "level": "HIGH",
    "id": 1
  },
  {
    "questions": [],
    "technology": "React Js",
    "type": "NON_CODE",
    "level": "MEDIUM",
    "id": 1
  }
]
removeItemIfConditationMatch()

Upvotes: 1

Related Questions