Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从多维数组(JS)中删除重复项_Javascript_Arrays_Filter_Splice - Fatal编程技术网

Javascript 从多维数组(JS)中删除重复项

Javascript 从多维数组(JS)中删除重复项,javascript,arrays,filter,splice,Javascript,Arrays,Filter,Splice,我有两个带有时间的数组,我想删除重复的数组,数组可以如下所示: arr1 = [ ['11:00','12:00','13:00'] ] arr2 = ['11:00'],['13:00'] for (i = 0; i < timeslotsBusy.length; i++) { for (j = 0; j < timeslotsBusy[i].length; j++) { console.log(timeslotsBusy[i][j]); } } 因此,我尝试使

我有两个带有时间的数组,我想删除重复的数组,数组可以如下所示:

arr1 = [ ['11:00','12:00','13:00'] ]
arr2 = ['11:00'],['13:00']
for (i = 0; i < timeslotsBusy.length; i++) {
  for (j = 0; j < timeslotsBusy[i].length; j++) {
    console.log(timeslotsBusy[i][j]);
  }
}
因此,我尝试使用2个for循环来遍历第二个数组,如下所示:

arr1 = [ ['11:00','12:00','13:00'] ]
arr2 = ['11:00'],['13:00']
for (i = 0; i < timeslotsBusy.length; i++) {
  for (j = 0; j < timeslotsBusy[i].length; j++) {
    console.log(timeslotsBusy[i][j]);
  }
}
for(i=0;i
这给了我数值:11:00,13:00

现在,如果值匹配,我尝试使用while循环拼接数组,但效果不太好。我也尝试过过滤方法,但没有成功

for (i = 0; i < timeslotsBusy.length; i++) {
  for (j = 0; j < timeslotsBusy[i].length; j++) {
    for (x = 0; x < timeslotsFree.length; x++) {
      timeslotsFree = timeslotsFree[x]
      timeslotsFree = timeslotsFree.filter(val => timeslotsBusy[i][j].includes(val))
    }
  }
}
for(i=0;itimeslotsBusy[i][j]。includes(val))
}
}
}
  • 输入数组(
    arr1
    arr2
    )是多维数组,因此需要使用
    array.prototype.flat()
    函数将其设置为1d数组

  • 进入
    1d数组后
    ,可以使用
    array.prototype.filter
    函数获取未复制的成员。(要检查arr2是否包含该项,可以使用
    Array.prototype.includes
    函数。)

const arr1=[[11:00'、'12:00'、'13:00'];
常数arr2=['11:00'],['13:00'];
常数arr1Flat=arr1.flat();
常量arr2Flat=arr2.flat();
常量输出=arr1Flat.filter((项)=>!arr2Flat.includes(项));

控制台日志(输出)
我试过了
<代码>我也试过了
。。。显示您所尝试的,您所显示的代码甚至没有试图解决问题请清理您的数组声明以匹配您的代码并显示预期的输出。您可以通过一个
filter()
调用和no for循环来实现这一点。