简体   繁体   English

使用ramda过滤掉唯一的嵌套值

[英]Filter out unique nested values using ramda

I have a collection of UNIX timestamps that looks like this: 我有一组UNIX时间戳,如下所示:

[
  {"start_time":1540458000000, "end_time":1540472400000}, 
  {"start_time":1540458000000, "end_time":1540486800000}, 
  {"start_time":1540458000000, "end_time":1540501200000}, 
  {"start_time":1540472400000, "end_time":1540486800000}, 
  {"start_time":1540472400000, "end_time":1540501200000}, 
  {"start_time":1540486800000, "end_time":1540501200000}
]

I'd like to pick out all the unique values from both start_time and end_time , so I'm left with: 我想从start_timeend_time选出所有唯一值,所以我留下:

[
  {"start_time":1540458000000}, 
  {"start_time":1540472400000}, 
  {"start_time":1540486800000}
  {"end_time":1540472400000},
  {"end_time":1540486800000}, 
  {"end_time":1540501200000}, 
]

I've looked at using something similar using groupBy , pluck , zipObj and more using the answer here . 我看使用类似的东西使用groupBypluckzipObj多使用这里的答案 But with no luck unfortunately. 但遗憾的是没有运气。

A nice-to-have would be a ramda function that worked without having to be given specific keys. 一个很好的将是一个ramda函数,无需给定特定的键即可工作。

Another Ramda approach: 另一个Ramda方法:

 const {pipe, map, toPairs, unnest, groupBy, head, uniqBy, last, values, apply, objOf} = R const uniqTimes = pipe( map(toPairs), //=> [[['start', 1], ['end', 2]], [['start', 1], ['end', 3]], ...] unnest, //=> [['start', 1], ['end', 2], ['start', 1], ['end', 3], ...] groupBy(head), //=> {start: [['start', 1], ['start', 1], ['start', 4], ...], end: [['end', 2], ...]} map(uniqBy(last)), //=> {start: [['start', 1], ['start', 4], ...], end: [['end', 2], ...]} values, //=> [[['start', 1], ['start', 4], ...], [['end', 2], ...]] unnest, //=> [['start', 1], ['start', 4], ..., ['end', 2], ...] map(apply(objOf)) //=> [{"start": 1}, {"start": 4}, ..., {"end": 2}, ...] ) const timestamps = [{"start_time":1540458000000,"end_time":1540472400000},{"start_time":1540458000000,"end_time":1540486800000},{"start_time":1540458000000,"end_time":1540501200000},{"start_time":1540472400000,"end_time":1540486800000},{"start_time":1540472400000,"end_time":1540501200000},{"start_time":1540486800000,"end_time":1540501200000}] console.log(uniqTimes(timestamps)) 
 <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script> 

This is how I like to use Ramda: build a pipeline of functions that each do simple transformations. 这就是我喜欢使用Ramda的方法:构建一个函数管道,每个函数都进行简单的转换。


Update 更新

A comment asked how to generate an output more like 评论询问如何生成更像的输出

{
   "start_time": [1540458000000, 1540458000000],
   "end_time": [1540472400000, 1540486800000]
}

This should do that for the same input: 这应该为相同的输入做到这一点:

const uniqTimes = pipe(
  map(toPairs), 
  unnest,
  groupBy(head),
  map(map(last)),
  map(uniq)
)

Not sure about ramda, but below plain js function will do that 不确定ramda,但在普通的js函数下面会这样做

 const arr = [ {"start_time":1540458000000, "end_time":1540472400000}, {"start_time":1540458000000, "end_time":1540486800000}, {"start_time":1540458000000, "end_time":1540501200000}, {"start_time":1540472400000, "end_time":1540486800000}, {"start_time":1540472400000, "end_time":1540501200000}, {"start_time":1540486800000, "end_time":1540501200000} ]; function foo(arr) { return [...arr.reduce((a, b) => { Object.entries(b).forEach(e => a.set(String(e), e)); return a; }, new Map())].map(([_,e]) => ({ [e[0]]: e[1] })) } console.log(foo(arr)); 

If the properties you want are unknown, but appear in all objects, you can convert each object to pairs, transpose the resulting array, get the unique values of each array, unnest them to a single array, and then convert back to objects: 如果所需的属性未知但出现在所有对象中,则可以将每个对象转换为对,转置结果数组,获取每个数组的唯一值,将它们排除在单个数组中,然后转换回对象:

 const { pipe, map, toPairs, transpose, uniqBy, last, unnest, objOf, apply } = R; const data = [ {"start_time":1540458000000, "end_time":1540472400000}, {"start_time":1540458000000, "end_time":1540486800000}, {"start_time":1540458000000, "end_time":1540501200000}, {"start_time":1540472400000, "end_time":1540486800000}, {"start_time":1540472400000, "end_time":1540501200000}, {"start_time":1540486800000, "end_time":1540501200000} ]; const getUniqueProps = pipe( map(toPairs), transpose, map(uniqBy(last)), unnest, map(apply(objOf)) ); console.log(getUniqueProps(data)); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script> 

If you know the properties you want, you can groupBy a property, get the first object from each group, and then pick the property you want from each object: 如果你知道你想要的属性,你可以groupBy的属性,得到各组的第一个对象,然后接你从每个对象所需的属性:

 const { groupBy, prop, concat, pipe, map, head, pick, values } = R; const data = [ {"start_time":1540458000000, "end_time":1540472400000}, {"start_time":1540458000000, "end_time":1540486800000}, {"start_time":1540458000000, "end_time":1540501200000}, {"start_time":1540472400000, "end_time":1540486800000}, {"start_time":1540472400000, "end_time":1540501200000}, {"start_time":1540486800000, "end_time":1540501200000} ]; const getUniqueProp = (propName) => pipe( groupBy(prop(propName)), map(pipe(head, pick([propName]))), values, ); const getStartEnd = (data) => concat( getUniqueProp('start_time')(data), getUniqueProp('end_time')(data), ); console.log(getStartEnd(data)); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script> 

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

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