简体   繁体   English

Javascript:过滤器数组 object 删除所有对象

[英]Javascript : filter array of object remove all objects

I got a simple question, been hours on it and can't figuring out the solution.我有一个简单的问题,花了几个小时,无法找出解决方案。 I got an array of object.我得到了一组 object。 I want to remove the object that has same values that a variable I pass.我想删除与我传递的变量具有相同值的 object。

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.我收到来自 select 表单的值。 For exemple I got myArray = [{id: 1, value: 12},{id: 2, value: 12},{id: 5, value: 12}] and variable = {id: 2, value: 12}例如,我得到了myArray = [{id: 1, value: 12},{id: 2, value: 12},{id: 5, value: 12}]variable = {id: 2, value: 12}

What I did wrong?我做错了什么?

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.这意味着如果idvalue相同,那么它将被过滤。

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
}

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

This is a failed DeMorgans Theorem.这是一个失败的德摩根定理。 You want the opposite of obj.id === variable.id && obj.value === variable.value , which is .(obj.id === variable.id && obj.value === variable.value) .你想要obj.id === variable.id && obj.value === variable.value的反义词,即.(obj.id === variable.id && obj.value === variable.value)

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM