检查两个数组中各个元素之间的值 [英] Checking values between individual elements in two arrays

查看:56
本文介绍了检查两个数组中各个元素之间的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to remove specific elements from an array based on data I'm getting from an API. The API returns an array of objects like this {"videoDate":"07/31/2020","videoTime":"1:00 AM"}. I have an existing array with items that look like this "07/31/2020 1:00 AM". My intention is to check if the existing array contains an item with a string matching both the videoDate and videoTime strings from the object and remove them.

        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 中的每个元素.对于上下文,响应包含约50个相同的对象 [{"videoTime" ::"07/31/2020 1:00 AM"} 以进行测试.day数组包含30个字符串,它们的格式相同,但值不同.该数组中有一个字符串,其确切值为"07/31/2020 1:00 AM" .我的目的是让该函数删除该条目,并保留所有其余条目以验证其是否有效.现在,它正在从 dayArray 中删除所有元素.

The above code is removing every element from the dayArray. For context, the response contains ~50 identical objects [{"videoTime":"07/31/2020 1:00 AM"} for testing. The day array contains 30 strings all with the same format but different values. There is one string in this array with the exact value of "07/31/2020 1:00 AM". My intention is to have the function delete this entry and leave all the remaining ones to verify that it is working. Right now it is removing every element from the dayArray.

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

This is what the dayArray initially looks like before the function is run:

["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响应如下所示.

The API response looks like this.

[{"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"}]

推荐答案

对要迭代的数组进行拼接将无法进行,因为所有数组索引都向下移动,并且迭代会跳过将元素移动到其所在位置的元素已选中.

Splicing the array that you're iterating over will not work, because all the array indexes move down and the iteration skips the element that moves into the place it just checked.

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

这篇关于检查两个数组中各个元素之间的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆