我需要比较两个对象数组(源/目标)的内容。
如果目标中不存在源中的项目,则应仅添加要包含的项目。
如果源中的项目确实存在于目标中,则我应该添加该项目以排除。
当前使用ramda R.differenceWith()
,但是当目标为空时出现问题。
我想知道differenceWith
是否适合这里的海豚,或者我可以使用其他功能。请提供一个示例谢谢!
注意:即使不使用ramda,也可以得到答案。
// source
const pathHash1 = {
hash: "c4ca4238a0b923820dcc509a6f75849b",
path: "./source/file1.txt"
};
const pathHash2 = {
hash: "c81e728d9d4c2f636f067f89cc14862c",
path: "./source/file2.txt"
};
const pathHash3 = {
hash: "eccbc87e4b5ce2fe28308fd9f2a7baf3",
path: "./souce/file3.txt"
};
// target
const pathHash4 = {
hash: "eccbc87e4b5ce2fe28308fd9f2a7baf3",
path: "./target/file3.txt"
};
// works
// const source = [pathHash1, pathHash2, pathHash3]
// const target = [pathHash4]
// does no work
const source = [pathHash1, pathHash2, pathHash3]
const target = []
// result pathHash1, pathHash2
const resultInclude = R.differenceWith((x,y)=> x.hash === y.hash, source, target)
const resultExclude= R.differenceWith((x,y)=> x.hash !== y.hash, source, target)
console.log('include',resultInclude.map(x=>x.hash))
console.log('exclude',resultExclude.map(x=>x.hash))
答案 0 :(得分:3)
在排除情况下使用R.innerJoin
:
const getInclude = R.differenceWith(R.eqProps('hash'))
const getExclude = R.innerJoin(R.eqProps('hash'))
const pathHash1 = {hash: "c4ca4238a0b923820dcc509a6f75849b",path: "./source/file1.txt"},pathHash2 = {hash: "c81e728d9d4c2f636f067f89cc14862c",path: "./source/file2.txt"},pathHash3 = {hash: "eccbc87e4b5ce2fe28308fd9f2a7baf3",path: "./souce/file3.txt"},pathHash4 = {hash: "eccbc87e4b5ce2fe28308fd9f2a7baf3",path: "./target/file3.txt"}
const source1 = [pathHash1, pathHash2, pathHash3]
const target1 = [pathHash4]
const source2 = [pathHash1, pathHash2, pathHash3]
const target2 = []
const getHash = R.map(R.prop('hash'))
console.log('include1', getHash(getInclude(source1, target1)))
console.log('exclude1', getHash(getExclude(source1, target1)))
console.log('include2', getHash(getInclude(source2, target2)))
console.log('exclude2', getHash(getExclude(source2, target2)))
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>
答案 1 :(得分:1)
在普通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));
答案 2 :(得分:0)
我认为您可以将difference
代替differenceWith
用于简单对象。如果要在source
和target
中都找到通用对象,建议使用innerJoin
:
const {difference, innerJoin, equals} = R;
const a = [{x: 1}, {y: 2}, {z: 3}];
const b = [{a: 0}, {x: 1}];
console.log(
difference(a, b)
);
console.log(
innerJoin(equals, a, b)
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>
答案 3 :(得分:0)
您从R.differenceWith()获得的结果是正确的,因为这种方法的来源是:
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;
});
就像您看到的那样,差异是使用 _includesWith 计算的。但是,当第二个数组为空时,正在进行的输出数组将被填充(没有重复项)。
// source
const pathHash1 = {
hash: "c4ca4238a0b923820dcc509a6f75849b",
path: "./source/file1.txt"
};
const pathHash2 = {
hash: "c81e728d9d4c2f636f067f89cc14862c",
path: "./source/file2.txt"
};
const pathHash3 = {
hash: "eccbc87e4b5ce2fe28308fd9f2a7baf3",
path: "./souce/file3.txt"
};
// target
const pathHash4 = {
hash: "eccbc87e4b5ce2fe28308fd9f2a7baf3",
path: "./target/file3.txt"
};
// works
// const source = [pathHash1, pathHash2, pathHash3]
// const target = [pathHash4]
// issue
const source = [pathHash1, pathHash2, pathHash3]
const target = []
// result pathHash1, pathHash2
const resultInclude = R.differenceWith((x,y)=> x.hash === y.hash, source, target)
const resultExclude= R.differenceWith((x,y)=> x.hash !== y.hash, source, target)
console.log('include',resultInclude.map(x=>x.hash))
console.log('exclude',resultExclude.map(x=>x.hash))
<script src="https://cdn.jsdelivr.net/npm/ramda@latest/dist/ramda.min.js"></script>
答案 4 :(得分:0)
这里的另一种选择是建立目标哈希的本机Set
并使用R.partition
根据{{1}中是否存在哈希将源列表分为两个列表}。
Set
const source = [
{
hash: "c4ca4238a0b923820dcc509a6f75849b",
path: "./source/file1.txt"
},
{
hash: "c81e728d9d4c2f636f067f89cc14862c",
path: "./source/file2.txt"
},
{
hash: "eccbc87e4b5ce2fe28308fd9f2a7baf3",
path: "./souce/file3.txt"
}
]
const target = [
{
hash: "eccbc87e4b5ce2fe28308fd9f2a7baf3",
path: "./target/file3.txt"
}
]
////
const targetHashes =
target.reduce((hashes, next) => hashes.add(next.hash), new Set)
const [resultExclude, resultInclude] =
R.partition(x => targetHashes.has(x.hash), source)
////
console.log("resultInclude", resultInclude)
console.log("resultExclude", resultExclude)