首页 > 解决方案 > 检查两个数组中各个元素之间的值

问题描述

我正在尝试根据从 API 获取的数据从数组中删除特定元素。API 返回一个对象数组,例如 {"videoDate":"07/31/2020","videoTime":"1:00 AM"}。我有一个现有数组,其中的项目看起来像“07/31/2020 1:00 AM”。我的目的是检查现有数组是否包含一个字符串,该字符串与对象中的 videoDate 和 videoTime 字符串匹配,然后将其删除。

        let responseArray = JSON.parse(response);
        dayArray.forEach((day) => {
          responseArray.forEach((res) => {
            if (day === res.videoTime) { 
              console.log('match');
              let index = dayArray.indexOf(day);
              dayArray.splice(index, 1);
            }
          })
        })

上面的代码是从dayArray. 对于上下文,response包含约 50 个相同[{"videoTime":"07/31/2020 1:00 AM"}的测试对象。day 数组包含 30 个格式相同但值不同的字符串。此数组中有一个字符串,其确切值为"07/31/2020 1:00 AM"。我的意图是让该功能删除此条目并保留所有其余条目以验证它是否正常工作。现在它正在从dayArray.

编辑:

这是函数运行之前 dayArray 最初的样子:

["07/31/2020 1:00 AM", "08/06/2020 9:00AM", "08/06/2020 10:00AM", "08/06/2020 11:00AM", "08/06/2020 1:00PM", "08/10/2020 9:00AM", "08/10/2020 10:00AM", "08/10/2020 11:00AM", "08/10/2020 1:00PM", "08/11/2020 9:00AM", "08/11/2020 10:00AM", "08/11/2020 11:00AM", "08/11/2020 1:00PM", "08/12/2020 9:00AM", "08/12/2020 10:00AM", "08/12/2020 11:00AM", "08/12/2020 1:00PM", "08/13/2020 9:00AM", "08/13/2020 10:00AM", "08/13/2020 11:00AM", "08/13/2020 1:00PM", "08/17/2020 9:00AM", "08/17/2020 10:00AM", "08/17/2020 11:00AM", "08/17/2020 1:00PM", "08/18/2020 9:00AM", "08/18/2020 10:00AM", "08/18/2020 11:00AM", "08/18/2020 1:00PM"]

API 响应如下所示。

[{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"}]

标签: javascriptarraysforeachsplice

解决方案


拼接您正在迭代的数组将不起作用,因为所有数组索引都向下移动,并且迭代会跳过移动到刚刚检查的位置的元素。

改为使用filter()

let dayArray = ["07/31/2020 1:00 AM", "08/06/2020 9:00AM", "08/06/2020 10:00AM", "08/06/2020 11:00AM", "08/06/2020 1:00PM", "08/10/2020 9:00AM", "08/10/2020 10:00AM", "08/10/2020 11:00AM", "08/10/2020 1:00PM", "08/11/2020 9:00AM", "08/11/2020 10:00AM", "08/11/2020 11:00AM", "08/11/2020 1:00PM", "08/12/2020 9:00AM", "08/12/2020 10:00AM", "08/12/2020 11:00AM", "08/12/2020 1:00PM", "08/13/2020 9:00AM", "08/13/2020 10:00AM", "08/13/2020 11:00AM", "08/13/2020 1:00PM", "08/17/2020 9:00AM", "08/17/2020 10:00AM", "08/17/2020 11:00AM", "08/17/2020 1:00PM", "08/18/2020 9:00AM", "08/18/2020 10:00AM", "08/18/2020 11:00AM", "08/18/2020 1:00PM"];
let responseArray = [{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"},{"videoTime":"07/31/2020 1:00 AM"}];

dayArray = dayArray.filter(date_time => !responseArray.find(({videoTime}) => videoTime == date_time));
console.log(dayArray);


推荐阅读