【问题标题】:How to filter an Array with another Array如何用另一个数组过滤一个数组
【发布时间】:2020-08-12 16:11:48
【问题描述】:

我有一个对象数组:

const array = [{id: 1, bar: "test" }, {id: 2, bar: "test2" }, {id: 3, bar: "test3" }]

我有第二个数组,其中包含我想从第一个数组中过滤掉的 ID:

const ids = [1, 2]

如果没有在ids 中找到 ID,如何创建新的对象数组。

【问题讨论】:

  • 你想要一个新数组,还是改变现有数组?
  • 每当有一个问题询问如何对数组执行删除操作时,您最终都会得到一堆答案,说明如何创建一个新数组并覆盖原始数组。 这些不是等效的操作。

标签: javascript arrays filter


【解决方案1】:

这是一个相当简单的filter 操作

const array = [{id: 1, bar: "test" }, {id: 2, bar: "test2" }, {id: 3, bar: "test3" }];

const ids = [1, 2];

var result = array.filter( x => !ids.includes(x.id));
console.log(result);

【讨论】:

    【解决方案2】:

    如果你需要改变原始数组,你可以这样做:

    const array = [{id: 1, bar: "test" }, {id: 2, bar: "test2" }, {id: 3, bar: "test3" }];
    
    const ids = [1, 2];
    
    ids.forEach(idToDelete => {
        const index = array.findIndex(({ id }) => id === idToDelete);
        array.splice(index, 1);
    });
    
    console.log(array);

    如果你需要一个新数组,你可以这样做:

    const array = [{id: 1, bar: "test" }, {id: 2, bar: "test2" }, {id: 3, bar: "test3" }];
    
    const ids = [1, 2];
    
    const result = array.filter(({ id }) => !ids.includes(id));
    
    console.log(result);

    您还可以将新数组重新分配给 array 变量:

    let array = [{id: 1, bar: "test" }, {id: 2, bar: "test2" }, {id: 3, bar: "test3" }];
    
    const ids = [1, 2];
    
    array = array.filter(({ id }) => !ids.includes(id));
    
    console.log(array);

    【讨论】:

    • 为什么.find()这个对象,然后再回去获取它的索引呢?这是所需工作量的两倍。使用.findIndexx() 或使用传统循环,
    • 如果您打算这样做,至少使用findIndex,以节省必须为ids中的每个元素枚举两次
    【解决方案3】:

    使用Array.filter

    let array = [
      {id: 1, bar: "test" },
      {id: 2, bar: "test2" },
      {id: 3, bar: "test3" }
    ];
    
    let ids = [1,2];
    
    let filteredArray = array.filter(row=>!ids.includes(row.id));
    
    console.log(filteredArray);

    【讨论】:

    • 您不会从第一个数组中删除任何内容。也许 OP 可以创建一个新数组,但问题确实要求对原始数组进行突变。
    • @slappy 只需使用 array 变量而不是 filteredArray 如果您需要从内存中处理主数组。
    • 如果 OP 对原始数组有多个引用怎么办?你不应该假设当他们要求突变时覆盖一个数组就足够了。
    • 为什么我不应该这样做?如果不是更多,使用 findIndex 和 splice 遍历数组的成本似乎与过滤器相同。
    • 这与性能无关;这是它们不是等效的操作。如果 OP 有多个对 array 的引用,并且您用更改覆盖其中一个,则其他引用将看不到更新。
    【解决方案4】:

    使用 lodash 的这个 oneliner。

    const _ = require("lodash");
    let filteredArray = _.remove(array, el=>[1,2].includes(el.id))
    

    【讨论】:

      【解决方案5】:

      使用filterindexOf

      const arr = [{ id: 1, bar: 'test' }, { id: 2, bar: 'test2' }, { id: 3, bar: 'test3' }];
      const ids = [1, 2];
      
      const result = arr.filter(element => ids.indexOf(element.id) === -1);
      
      console.log(result);

      【讨论】:

        【解决方案6】:

        我们可以在 JavaScript 中使用 Array filter() 过滤数组

        const myArray = [{id: 1, bar: "test" }, {id: 2, bar: "test2" }, {id: 3, bar: "test3" }]
        const ids = [1,2]
        
        const resultArray = myArray.filter(item => !ids.includes(item.id));
        console.log(resultArray);
        

        【讨论】:

        • 那些双引号()不是",会抛出错误。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-08
        相关资源
        最近更新 更多