我正在尝试根据从 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 响应如下所示。
蝴蝶不菲
相关分类