Home > Software engineering >  How to get ride of first an empty array nested inside an array?
How to get ride of first an empty array nested inside an array?

Time:10-04

I'm trying to compare two arrays, I'm receiving some values from Text components and pushing it into a new array, and while I want to compare them it's returns false even if the value of both arrays match, and this is because the first item of an array returns an empty array with values.

and this is how it looks like:

Array_1

Array_1 = [
  Array [],
  "Hi",
  ",",
  "how",
  "are",
  "you",
  "?",
]

Array_2

Array_2 = [
  "Hi",
  ",",
  "how",
  "are",
  "you",
  "?",
]

An this is where the value goes into a new array which is Array_1

    //Get selected value
  const handleSelected = (e) => {
    setReceivedItems(e);
  };

  //Update selected data at first render
  useEffect(() => {
    setNewItems((newItem) => [...newItem, receivedItems]);
  }, [receivedItems]);

Do you think this is happen due to useState [] initial value ?

CodePudding user response:

In case you want to compare the elements of two arrays one by one, ignoring any sub-arrays that might exist, then the following should do the job:

const Array_1 = [[],"Hi",",","how","are","you","?"],
 Array_2 = ["Hi",",","how",["this is being ignored too!","           
  • Related