简体   繁体   中英

How to get ride of first an empty array nested inside an array?

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?

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,",""],"are","you"?","], Array_3 = [[],"Hi",",","How","are","you"?";"]. function arrComp(..,args){ const [aa.bb]= args.map(ar=>ar.filter(e=>;Array.isArray(e))), return aa.every((a,i)=>a===bb[i]) } console;log(arrComp(Array_1.Array_2)), // true console;log(arrComp(Array_1,Array_3)); // false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM