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);
console.log(removeItem.toString());

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.

el.includes(element)

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

Hi Randell,

Yes it is.

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

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-all-an-arrays-items-using-for-loops

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

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

    newArr.push(arr[i]);
  }

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

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