What would be the best way to do this?

Hi All,

What would be the best way to return nested arrays filtered with element removed?

function remove(arr, element) {
  return arr.filter(el => el !== element);

const arr = [[3, 4, 5], [6, 7, 8], [9, 0, 6]];

const removeItem = remove(arr, 6);

Desired output:

[[3, 4, 5], [7, 8], [9, 0]]

Consider that arr is an array of arrays, so el is an array. How do you check if an array contain something?

This doesn’t return desired output.


I might be wrong though on the .includes as to wether it’s the correct method to check for element.

Is this related to a challenge?

Hi Randell,

Yes it is.

Iterate Through All an Array’s Items Using For Loops in the Data Structures section.


I wanted to know how to return the nested arrays without the (element) instead of removing the whole nested array.

Try using another array method you have learned which iterates through all elements of arr and then use the filter method each element (a subarray) the same way you already tried.

Actually, you are supposed to be using a for loop for this particular challenge, so you should not be using high order functions if I am not mistaken.

1 Like

Yes thanks for that Randell. Always love your input and appreciate it very much.

Anyways, this has got way more complicated than it should be. I managed to fix this but i got the feeling it can be so much more cleaner code than what i came up with. How can this be refactored/beautified?

const remove = (arr, elem) => {
  let newArr = [];

  for (let i = 0; i < arr.length; i++) {
    let index = arr[i].indexOf(elem);

    if (elem === arr[i][index]) {
      arr[i].splice(index, 1);


  return newArr.filter(item => {
    return !item.includes(elem);

const arr = [[3, 4, 5, 6], [6, 7, 8], [9, 0, 6]];
console.log(remove(arr, 6));

If you have a working solution, then click on the Get a Hint link and take a look at the other solutions for inspiration.

1 Like
const remove = (arrs, element) => {
  return arrs.filter((arr) => {
    return arr.every((number) => number !== element);

You were almost there,. but you were checking an array against an element.

@kerafyrm02 The OP wanted a way to remove just the element specified from the sub arrays. Your solution above removes the entire subarray if the subarray has the element in it.

@Romson - See below for one possible way of doing what you requested.

const remove = (arr, element) => arr.map(subArr => subArr.filter(num => num !== element));

Yeah., I have a tendency for selective reading… lol. Good catch @RandellDawson

I think there is another challenge (can’t think of it right now) in which your solution works perfectly.