简体   繁体   English

循环遍历2个数组并稍稍发现差异

[英]Loop through 2 arrays and find differences with a twist

Ok so this is kind of a hard one for me to wrap my head around so im hoping that one of you guys might be able to help. 好的,这对我来说很难缠住我,所以我希望你们中的一个能够提供帮助。 A little context, this is for an updater im writing that grabs an xml list of files from a cdn, then compares that vs an older list to look for file differences so i know which files are out of date and need to be redownloaded. 有一点上下文,这是针对更新程序即时消息编写的,它从CDN抓取文件的xml列表,然后将其与较旧的列表进行比较以查找文件差异,因此我知道哪些文件已过期并且需要重新下载。 Right now i am unable to find a proper solution for this. 现在,我无法为此找到合适的解决方案。

Currently i have 3 arrays. 目前我有3个数组。 Array1, Array2, and DiffArray. Array1,Array2和DiffArray。 Array1 stores the xml entries from the CDN..ie the master Array2 stores the old entries of what we currently have..ie the slave Array3 stores the differences of what has changed between the 2. Array1存储来自CDN的xml条目。即,主Array2存储我们当前拥有的旧条目。.即,从Array3存储2之间的差异。

Heres a sample bit of info thats in each array. 这是每个数组中信息多数民众赞成的示例位。 Note that each new line is parsed into a separate index of their corresponding array 请注意,每一行都被解析成其对应数组的单独索引

Array1: 数组1:

cbt/ar/816.mp3
2019-06-05T16:40:33.212Z
cbt/ar/817.mp3
2019-06-05T16:40:31.509Z
cbt/ar/818.mp3
2019-04-05T16:40:30.978Z
cbt/ar/819.mp3
2019-04-05T16:40:29.807Z

Array2: 数组2:

cbt/ar/816.mp3
2019-04-05T16:40:33.212Z
cbt/ar/817.mp3
2019-04-05T16:40:31.509Z
cbt/ar/818.mp3
2019-04-05T16:40:30.978Z
cbt/ar/819.mp3
2019-04-05T16:40:29.807Z

a couple things to note: 1.) this is a list of a file name, and its last modified date 2.) As you can see, array1 has a new file for both 816.mp3 and 817.mp3 需要注意的几件事:1.)这是文件名的列表,以及文件的最后修改日期2.)如您所见,array1为816.mp3和817.mp3提供了一个新文件。

The idea is to see that, note which files are different, then redownload those files with the more current version. 这样做的目的是要注意哪些文件不同,然后使用最新版本重新下载这些文件。

This is what i currently have, but as you can see its not the right solution for the job: 这是我目前拥有的,但是如您所见,它不是适合该工作的正确解决方案:

var a = [];
      for (var x = 0; x < remoteArray.length; x++) {
        a[remoteArray[x]] = true;

      }

      for (var y = 0; y < localArray.length; y++) {
        if (a[localArray[y]]) {
          delete a[localArray[y]];
        } else {
          a[localArray[y]] = true;
        }
      }

      for (var z in a) {
        diffArray.push(z);
        log.info("::DIFFERENCES::" + z);
      }
    }

This current code only outputs the actual literal differences and doesn't really help me know which file is different so that i can update it 当前代码仅输出实际的文字差异,并不能真正帮助我知道哪个文件不同,因此我可以对其进行更新

Not sure if this is the format you want the result in, but it identifies which files need to be updated: 不确定这是否是您想要的结果格式,但是它确定需要更新哪些文件:

# Make a dictionary matching each cdn file to its timestamp
cdn = {}
for i in range(0,len(Array1),2):
    cdn[Array1[i]] = Array1[i+1]

# Make an array of files needing to be updated
update = []
for i in range(0,len(Array2),2):
    path = Array2[i]
    # If file is in CDN and the one there is newer, add it to update
    if path in cdn and cdn[path] > Array2[i+1]:
        update.append( path )

It might be easier to first transform your data into a list of objects representing each file. 首先将数据转换为代表每个文件的对象列表可能会更容易。 It is not the most performant approach but will make things clearer and easier to maintain. 这不是最高效的方法,但是会使事情更清晰,更易于维护。

function transformFilesList(array) {
    var files = [];
    for (var i = 0; i < array.length; i += 2) {
        files.push({
            name: array[i],
            modified: array[i + 1]
        });
    }
    return files;
}

var remote = transformFilesList(remoteArray);
var local = transformFilesList(localArray);
var needsDownload = remote.filter(file => {
    let match = local.find(localFile => localFile.name === file.name);
    // We need to download if there is no local file with this name or its modification date is older than the remote one
    return !match || match.modified < file.modified;
});

console.log('List of files to (re)download', needsDownload);
// Each file in this list will be an object { name, modified }

If you cannot use features like Array.prototype.filter or arrow functions (old browsers or Node versions), an old alternative to get needsDownload would be: 如果您无法使用Array.prototype.filter或箭头函数(旧的浏览器或Node版本)之类的功能,则获取needsDownload的旧替代方法是:

var needsDownload = [];
for (var i = 0; i < remote.length; i++) {
    var found;
    for (var j = 0; j < local.length; j++) {
        if (remote[i].name === local[j].length) {
            found = local[j];
            break;
        }
    }
    if (!found || found.modified < remote[i].modified) {
        needsDownload.push(found);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM