2

I got a simple question, been hours on it and can't figuring out the solution. I got an array of object. I want to remove the object that has same values that a variable I pass.

My problem is that it removes all the objects, even those that are not similar to my variable.

Here is my code

function appSelect(variable) {
    //we check if the object already exists in my array {id:x, value:y}
    const found = myArray.find(obj => {
        return (obj.id === variable.id && obj.value === variable.value);
    });
        
    if (found) { 
        //If find it, I want to remove it from my array

        const filtered = myArray.filter(obj => {
            return (obj.id !== variable.id && obj.value !== variable.value);
        })

    //Return empty array
}

I receive the value from a select form. For exemple I got myArray = [{id: 1, value: 12},{id: 2, value: 12},{id: 5, value: 12}] and variable = {id: 2, value: 12}

What I did wrong?

5
  • What if there are multiple elements in the array, whose id is different, but the value is the same as that of the variable you are passing? Commented Sep 10, 2022 at 8:09
  • That's the point. Isn't the && condition taking care of this ? In obj.id !== variable.id && obj.value !== variable.value I want to say that I want to return only object that have not same id AND not same value Commented Sep 10, 2022 at 8:12
  • 1
    can you share myArray content? Commented Sep 10, 2022 at 8:13
  • Please share a more complete minimal reproducible example enough for us to reproduce any issues. What are the values of myArray and variable? Commented Sep 10, 2022 at 8:16
  • myArray = [{id: 1, value: 12},{id: 2, value: 12},{id: 5, value: 12}] and variable = {id: 2, value: 12}. Commented Sep 10, 2022 at 8:16

4 Answers 4

2

The reason is the code below:

return (obj.id !== variable.id && obj.value !== variable.value)

Which means if id or value is the same,then it will be filtered.

You can change it to

return !(obj.id === variable.id && obj.value === variable.value)

Full code:

function appSelect(variable) {
    //we check if the object already exists in my array {id:x, value:y}
    const found = myArray.find(obj => {
        return (obj.id === variable.id && obj.value === variable.value);
    });
        
    if (found) { 
        //If find it, I want to remove it from my array

        const filtered = myArray.filter(obj => {
            return !(obj.id === variable.id && obj.value === variable.value);
        })

    //Return empty array
}
Sign up to request clarification or add additional context in comments.

Comments

1

This is a failed DeMorgan's Law. You want the opposite of obj.id === variable.id && obj.value === variable.value, which is !(obj.id === variable.id && obj.value === variable.value) or obj.id !== variable.id || obj.value !== variable.value when DeMorgan's Law is applied.

const filtered = myArray.filter((obj) => {
  return obj.id !== variable.id || obj.value !== variable.value;
});

Comments

0

if one of the value is different then it's not the same objet, note that the first found check is unnecessary

function appSelect(variable, arr) {
    return arr.filter(obj => (obj.id !== variable.id || obj.value !== variable.value))
}

Comments

0

You can try this code:

const myArray = [
  { id: 1, value: 12 },
  { id: 2, value: 12 },
  { id: 5, value: 12 },
];

const variable = { id: 2, value: 12 };

function appSelect(variable) {
  //we check if the object already exists in my array {id:x, value:y}
  const found = myArray.find((obj) => {
    return obj.id === variable.id && obj.value === variable.value;
  });

  if (found) {
    //If find it, I want to remove it from my array
    const filtered = myArray.filter((obj) => {
      // if you found the object variable in your array then you return nothing
      if (obj.id === variable.id && obj.value === variable.value) {
        return;
      }

      // if the current object is not object variable then return it
      return obj;
    });
    return filtered;
  }

  //Return empty array
  return myArray;
}

console.log(appSelect(variable));

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.