-1

I'm trying to filter an object of array localcartArr to exclude any object that's included in the products array, but it returns the same localcartArr without excluding this object:

{ "productID": "5f6fd57f8b6f6b0017992443" }

Which exists in products array, it works only with === operator, but doesn't work with the not equal operation != or !==.

const products = [
  {
    "_id": "60242abc209cbd32d8e85ec8",
    "productID": "5f6fd4f18b6f6b001799243f",
    "quantity": 2
  },
  {
    "_id": "60242b00209cbd32d8e85ec9",
    "productID": "5f6fd57f8b6f6b0017992443",
    "quantity": 1
  }
]
        
let localcartArr = [
  {                
    "productID": "5f6fd57f8b6f6b0017992443",
    "quantity": 2
  },
  {                   
    "productID": "5f6fd12a8b6f6b001799242f",
    "quantity": 1
  },
  {               
    "productID": "5f7a5668a9baa50017d495e8",
    "quantity": 1
  }
]

let filterd = localcartArr.filter(local => {
  return products.some(product => {
    return local.productID !==  product.productID    
  });
});
1

2 Answers 2

1

In order to invert "some / equal", you need to return "not every / not equal".

This is a principle of De Morgan's laws in boolean logic.

const products = [{
  "_id": "60242abc209cbd32d8e85ec8",
  "productID": "5f6fd4f18b6f6b001799243f",
  "quantity": 2
}, {
  "_id": "60242b00209cbd32d8e85ec9",
  "productID": "5f6fd57f8b6f6b0017992443",
  "quantity": 1
}];

const localcartArr = [
  { "productID": "5f6fd57f8b6f6b0017992443", "quantity": 2 },
  { "productID": "5f6fd12a8b6f6b001799242f", "quantity": 1 },
  { "productID": "5f7a5668a9baa50017d495e8", "quantity": 1 }
];

let filtered1 = localcartArr.filter(local =>
  products.some(product =>
    local.productID === product.productID));
    
let filtered2 = localcartArr.filter(local =>
  !products.every(product =>
    local.productID !== product.productID));

console.log(filtered1);
console.log(filtered2);
.as-console-wrapper { top: 0; max-height: 100% !important; }

Here is the same program, but simplified:

const products = [{
  "_id": "60242abc209cbd32d8e85ec8",
  "productID": "5f6fd4f18b6f6b001799243f",
  "quantity": 2
}, {
  "_id": "60242b00209cbd32d8e85ec9",
  "productID": "5f6fd57f8b6f6b0017992443",
  "quantity": 1
}];

const cart = [
  { "productID": "5f6fd57f8b6f6b0017992443", "quantity": 2 },
  { "productID": "5f6fd12a8b6f6b001799242f", "quantity": 1 },
  { "productID": "5f7a5668a9baa50017d495e8", "quantity": 1 }
];

const filterEqual = (a, b, c) => a.filter(x => b.some(y => (c(x) === c(y))));
const filterNotEqual = (a, b, c) => a.filter(x => !b.every(y => (c(x) !== c(y))));

const filtered1 = filterEqual(cart, products, ({productID}) => productID);
const filtered2 = filterNotEqual(cart, products, ({productID}) => productID);

console.log(filtered1);
console.log(filtered2);
.as-console-wrapper { top: 0; max-height: 100% !important; }

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

Comments

0

Filter using the property you want to match on and exclude that not matching.

Some examples:

const products = [{
    "_id": "60242abc209cbd32d8e85ec8",
    "productID": "5f6fd4f18b6f6b001799243f",
    "quantity": 2
  },
  {
    "_id": "60242b00209cbd32d8e85ec9",
    "productID": "5f6fd57f8b6f6b0017992443",
    "quantity": 1
  }
];

let localcartArr = [{
    "productID": "5f6fd57f8b6f6b0017992443",
    "quantity": 2
  },
  {
    "productID": "5f6fd12a8b6f6b001799242f",
    "quantity": 1
  },
  {
    "productID": "5f7a5668a9baa50017d495e8",
    "quantity": 1
  }
];
// find matches
const myArrayFiltered = localcartArr.filter((el) => {
  return products.some((f) => {
    return f.productID === el.productID;
  });
});

console.log(myArrayFiltered);

//filter out matches
const notIn = localcartArr.filter((el) => {
  return myArrayFiltered.some((f) => {
    return f.productID !== el.productID;
  });
});

console.log(notIn);

// to put it together (for clarity)
// not that this is not super efficient if you have a large pair of sets
const notInX = localcartArr.filter((el) => {
  return  localcartArr.filter((el) => {
  return products.some((f) => {
    return f.productID === el.productID;
  });
}).some((f) => {
    return f.productID !== el.productID;
  });
});
console.log(notInX);

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.