1

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.

4 Answers 4

4

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.

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

Comments

1

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

1 Comment

You are using && but I think you mean to use || or. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
1

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

Comments

0

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 &&

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.