为了账号安全,请及时绑定邮箱和手机立即绑定

检查两个数组中各个元素之间的值

检查两个数组中各个元素之间的值

月关宝盒 2022-12-22 09:32:03
我正在尝试根据从 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 响应如下所示。
查看完整描述

1 回答

?
蝴蝶不菲

TA贡献1810条经验 获得超4个赞

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


改用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);


查看完整回答
反对 回复 2022-12-22
  • 1 回答
  • 0 关注
  • 67 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信