Checking values between individual elements in two arrays

我与影子孤独终老i 提交于 2021-02-05 09:46:47

问题


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);
            }
          })
        })

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.

EDIT:

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"]

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

回答1:


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.

Use filter() instead.

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


来源:https://stackoverflow.com/questions/63272580/checking-values-between-individual-elements-in-two-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!