Javascript 比较两个数组的内容

Javascript 比较两个数组的内容,javascript,ramda.js,Javascript,Ramda.js,我需要比较对象的两个数组源/目标的内容 若源中的某个项在目标中不存在,我应该只添加要包含的项 若目标中确实存在源中的项,我应该只添加要排除的项 目前正在使用ramda R.differenceWith,但当目标为空时,我遇到了一个问题 我想知道这个差异是否适合这里的海豚,或者我可以使用其他函数。请给我举个例子,谢谢 注:即使不使用ramda也可以回答 我认为对于简单的对象,你可以使用差分而不是差分。如果您想在源和目标中找到公共对象,我建议使用innerJoin: 常量{差,innerJoin,

我需要比较对象的两个数组源/目标的内容

若源中的某个项在目标中不存在,我应该只添加要包含的项

若目标中确实存在源中的项,我应该只添加要排除的项

目前正在使用ramda R.differenceWith,但当目标为空时,我遇到了一个问题

我想知道这个差异是否适合这里的海豚,或者我可以使用其他函数。请给我举个例子,谢谢

注:即使不使用ramda也可以回答

我认为对于简单的对象,你可以使用差分而不是差分。如果您想在源和目标中找到公共对象,我建议使用innerJoin:

常量{差,innerJoin,等于}=R; 常数a=[{x:1},{y:2},{z:3}]; 常数b=[{a:0},{x:1}]; console.log 差异a,b ; console.log 这等于,a,b ; 我认为对于简单的对象,你可以使用差分而不是差分。如果您想在源和目标中找到公共对象,我建议使用innerJoin:

常量{差,innerJoin,等于}=R; 常数a=[{x:1},{y:2},{z:3}]; 常数b=[{a:0},{x:1}]; console.log 差异a,b ; console.log 这等于,a,b ;
在香草JS中,你可以考虑使用这个版本:

  const resultInclude = pathHashListSource.filter(x => !pathHashListTarget.find(y => y.hash === x.hash));
  const resultExclude = pathHashListSource.filter(x => pathHashListTarget.find(y => y.hash === x.hash));

在香草JS中,你可以考虑使用这个版本:

  const resultInclude = pathHashListSource.filter(x => !pathHashListTarget.find(y => y.hash === x.hash));
  const resultExclude = pathHashListSource.filter(x => pathHashListTarget.find(y => y.hash === x.hash));
您从中得到的结果是正确的,因为这种方法的来源是:

var differenceWith = _curry3(function differenceWith(pred, first, second) {
    var out = [];
    var idx = 0;
    var firstLen = first.length;
    while (idx < firstLen) {
            if (!_includesWith(pred, first[idx], second) && 
                !_includesWith(pred, first[idx], out)) {
                out.push(first[idx]);
            }
            idx += 1;
    }
    return out;
});
您从中得到的结果是正确的,因为这种方法的来源是:

var differenceWith = _curry3(function differenceWith(pred, first, second) {
    var out = [];
    var idx = 0;
    var firstLen = first.length;
    while (idx < firstLen) {
            if (!_includesWith(pred, first[idx], second) && 
                !_includesWith(pred, first[idx], out)) {
                out.push(first[idx]);
            }
            idx += 1;
    }
    return out;
});
用于排除情况:

const getInclude=R.differenceWithR.eqProps'hash' const getExclude=R.innerJoinR.eqProps'hash' const pathHash1={hash:c4ca4238a0b923820dcc509a6f75849b,路径:./source/file1.txt},pathHash2={hash:c81e728d9d4c2f636f067f89cc14862c,路径:./source/file2.txt},pathHash3={hash:eccbc87e4b5ce2fe28308fd9f2a7baf3,路径:./souce/file3.txt},pathHash4={hash:ECCBC87E4B5CE2FE28FDA7BAF3,路径:./file8303} 常量source1=[pathHash1、pathHash2、pathHash3] 常量target1=[pathHash4] 常量source2=[pathHash1,pathHash2,pathHash3] 常数target2=[] const getHash=R.mapR.prop'hash' console.log'include1',getHashgetIncludesource1,target1 console.log'exclude1',getHashgetExcludesource1,target1 console.log'include2',getHashgetIncludesource2,target2 console.log'exclude2',getHashgetExcludesource2,target2 用于排除情况:

const getInclude=R.differenceWithR.eqProps'hash' const getExclude=R.innerJoinR.eqProps'hash' const pathHash1={hash:c4ca4238a0b923820dcc509a6f75849b,路径:./source/file1.txt},pathHash2={hash:c81e728d9d4c2f636f067f89cc14862c,路径:./source/file2.txt},pathHash3={hash:eccbc87e4b5ce2fe28308fd9f2a7baf3,路径:./souce/file3.txt},pathHash4={hash:ECCBC87E4B5CE2FE28FDA7BAF3,路径:./file8303} 常量source1=[pathHash1、pathHash2、pathHash3] 常量target1=[pathHash4] 常量source2=[pathHash1,pathHash2,pathHash3] 常数target2=[] const getHash=R.mapR.prop'hash' console.log'include1',getHashgetIncludesource1,target1 console.log'exclude1',getHashgetExcludesource1,target1 console.log'include2',getHashgetIncludesource2,target2 console.log'exclude2',getHashgetExcludesource2,target2
这里的另一个选项是建立目标哈希的本机集合,并根据集合中是否存在哈希,使用将源列表拆分为两个列表

常量源=[ { 散列:c4ca4238a0b923820dcc509a6f75849b, 路径:./source/file1.txt }, { 哈希:c81e728d9d4c2f636f067f89cc14862c, 路径:./source/file2.txt }, { 哈希:eccbc87e4b5ce2fe28308fd9f2a7baf3, 路径:./souce/file3.txt } ] 常数目标=[ { 哈希:eccbc87e4b5ce2fe28308fd9f2a7baf3, 路径:./target/file3.txt } ] //// 康斯特塔格萨斯酒店= target.reducehash,next=>hashes.addnext.hash,新集合 常量[resultExclude,resultInclude]= R.partitionx=>targethasts.hasx.hash,源 //// console.logresultInclude,resultInclude console.logresultExclude,resultExclude
这里的另一个选项是建立目标哈希的本机集合,并根据集合中是否存在哈希,使用将源列表拆分为两个列表

常量源=[ { 散列:c4ca4238a0b923820dcc509a6f75849b, 路径:./source/file1.txt }, { 哈希:c81e728d9d4c2f636f067f89cc14862c, 路径:./source/file2.txt }, { 哈希:eccbc87e4b5ce2fe28308fd9f2a7baf3, 路径:./souce/file3.txt } ] 常数目标=[ { 哈希:eccbc87e4b5ce2fe28308fd9f2a7baf3, 路径:./target/file3.txt } ] //// 康斯特塔格萨斯酒店= target.reducehash,next=>hashes.addnext.hash,新集合 常量[resultExclude,resultInclude]= R.partitionx=>targethasts.hasx.hash,源 //// console.logresultInclude,resultInclude console.logresultExclude,resultExclude
这应该是一个公认的答案。我真的很喜欢拉姆达,但这是最干净、最不冗长的索尔
这是一个可以接受的答案。我真的很喜欢ramda,但这是这里最干净、最不冗长的解决方案。