简体   繁体   English

如何首先乘坐嵌套在数组内的空数组?

[英]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.我正在尝试比较两个 arrays,我从文本组件接收一些值并将其推入一个新数组,当我想比较它们时它返回 false,即使两个 arrays 的值匹配,这是因为数组的第一项返回一个包含值的空数组。

and this is how it looks like:这就是它的样子:

Array_1数组_1

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

Array_2 Array_2

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

An this is where the value goes into a new array which is Array_1这是值进入新数组 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?你认为这是由于 useState [] 初始值而发生的吗?

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:如果你想一个一个地比较两个 arrays 的元素,忽略任何可能存在的子数组,那么下面应该做的工作:

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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