How to filter an Array with another Array

Viewed 3019

I have an Array of Objects:

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

I have a second array containing the ID's that I want to filter out of the first Array:

const ids = [1, 2]

How do I create a new Array of Objects without the ID's found in ids.

7 Answers

This is a fairly simple filter operation

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

If you need to mutate the original array you can do like this:

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

If you need a new array you can do like this:

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

You could also reassign a new array to the array variable:

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

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

Use this oneliner from lodash.

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

Use filter and indexOf.

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

We can filter an array in JavaScript using 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);

In term of performance the best solution will be the next one:

let array = [{id: 1, bar: "test" }, {id: 2, bar: "test2" }, {id: 3, bar: "test3" }];
const ids = [1,2];
const idSet = new Set();
for (const id of ids) {
 idSet.add(id);
}

array = array.filter(x => !set.has(x.id));
//const newArray if you need the initial array unmodified

In this case we perform two consequencial iteration instead of a nested one, so the time complexity will be O(n) instead of O(n^2);

##Edit

If you instead need the initial array to be mutated and not overwritten you can use this approach:

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

    for (const id of ids) {
    const index = array.findIndex(x => x.id == id);
    array.splice(index, 1);

}



In the second case the time complexity will be O(n*m), where n is array length and m is ids length.

Related