给定此数组,包含javascript对象(json):
每个对象都有一个b
property和一个u
属性,
(每个都包含我不关心此练习的其他属性)。
[
{ "b": "A", "u": "F", ... },
{ "b": "M", "u": "T", ... },
{ "b": "A", "u": "F", ... },
{ "b": "M", "u": "T", ... },
{ "b": "M", "u": "T", ... },
{ "b": "X", "u": "Y", ... },
{ "b": "X", "u": "G", ... },
]
我想使用ramda来查找所有重复项的集合。结果看起来应该是这样的。
[
{ "b": "A", "u":"F" },
{ "b": "M", "u":"T" }
]
这两个条目具有重复项,它们分别在原始列表中重复2次和3次。
编辑
我找到了一个使用underscore的解决方案,它保留了原始数组元素,并将它们完美地分成单个和重复。我更喜欢ramda.js,并且下划线不仅仅提供一组重复 - 根据问题,所以我将问题保持开放,直到有人可以使用ramda回答。我继续用下划线直到问题得到解答。
我有一个repl找到了独特的价值......作为一个开始......
这似乎过于复杂,不太可能具有高性能,但有一种选择:
const foo = pipe(
project(['b', 'u']),
reduce(
({results, foundOnce}, item) => contains(item, results)
? {results, foundOnce}
: contains(item, foundOnce)
? {results: append(item, results), foundOnce}
: {results, foundOnce: append(item, foundOnce)},
{results: [], foundOnce: []}
),
prop('results')
)
foo(xs); //=> [{b: 'A', u: 'F'}, {b: 'M', u: 'T'}]
也许这个版本更容易理解,但需要额外迭代数据:
const foo = pipe(
project(['b', 'u']),
reduce(
({results, foundOnce}, item) => contains(item, foundOnce)
? {results: append(item, results), foundOnce}
: {results, foundOnce: append(item, foundOnce)},
{results: [], foundOnce: []}
),
prop('results'),
uniq
)
如果你不关心多次循环数据,你可能会这样:
pick
(您自己的想法)创建仅包含相关道具的部分副本groupBy
和hash
函数对相似的对象进行分组。 (Alternatively:sort
首先使用groupWith(equals)
)values
获取分组数组filter
过滤掉只有1个项目(那些没有被欺骗......)的数组map(head)
返回每个数组的第一个元素在代码中:
const containsMoreThanOne = compose(lt(1), length);
const hash = JSON.stringify; // Naive.. watch out for key-order!
const getDups = pipe(
map(pick(["b", "u"])),
groupBy(hash),
values,
filter(containsMoreThanOne),
map(head)
);
getDups(data);
Ramda REPL的工作演示。
一种更混合的方法是在一个减速器中扼杀所有这些逻辑,但它对我来说看起来有点混乱......
const clean = pick(["b", "u"]);
const hash = JSON.stringify;
const dupReducer = hash => (acc, o) => {
const h = hash(o);
// Mutate internal state
acc.done[h] = (acc.done[h] || 0) + 1;
if (acc.done[h] === 2) acc.result.push(o);
return acc;
};
const getDups = (clean, hash, data) =>
reduce(dupReducer(hash), { result: [], done: { } }, map(clean, data)).result;
getDups(clean, hash, data);
const arr = [];
const duplicates = [];
const values1 = [
{ b: 'A', u: 'F', a: 'q' },
{ b: 'M', u: 'T', a: 'q' },
{ b: 'A', u: 'F', a: 'q' },
{ b: 'M', u: 'T', a: 'q' },
{ b: 'M', u: 'T', a: 'q' },
{ b: 'X', u: 'Y', a: 'q' },
{ b: 'X', u: 'G', a: 'q' },
];
values1.forEach(eachValue => {
arr.push(values(pick(['b', 'u'], eachValue)));
});
arr.forEach(fish => {
if ( indexOf(fish, arr) !== lastIndexOf(fish, arr) ) {
duplicates.push(zipObj(['b', 'u'], fish));
}
});
[blog]: https://ramdafunctionsexamples.com/ "click here for updates"
<https://ramdafunctionsexamples.com/>?
不是Ramda JS的专家,但我认为以下内容应该有效:
var p = [
{ "b": "A", "u": "F" },
{ "b": "A", "u": "F" },
{ "b": "A", "u": "F" },
{ "b": "A", "u": "F" },
{ "b": "A", "u": "F" },
{ "b": "M", "u": "T" }
];
var dupl = n => n > 1;
R.compose(
R.map(JSON.parse),
R.keys,
R.filter(dupl),
R.countBy(String),
R.map(JSON.stringify)
)(p)
如果有,请告诉我。