Home > Mobile >  Javascript : filter array of object remove all objects
Javascript : filter array of object remove all objects

Time:09-11

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?

CodePudding user response:

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
}

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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));
  • Related