-3

suppose, I have array let arr = [0,1,[2],3,4]

i want to remove [2] from arr array by only value [2].

expected array would be arr = [0,1,3,4]

i appreciate your help, thank you.

2
  • You can do like this arr.filter(x=>typeof(x)!="object") Commented Mar 1, 2021 at 9:11
  • @SourabhSomani I guess the OP doesn't want to simply remove all objects Commented Mar 1, 2021 at 9:19

1 Answer 1

0

This should help!

let arr = [0, 1, [2], 3, 4, 'A', [2], { name: 'John' }];

const match = (a1, a2) => {
  if (a1.length !== a2.length) return false;
  for (let i = 0; i < a1.length; i++) {
    if (a1[i] !== a2[i]) return false;
  }
  return true;
};

const target = [2];
arr = arr.filter((el) => !match(el, target));

console.log(arr);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.