[英]Filter out unique nested values using ramda
我有一組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}
]
我想從start_time
和end_time
選出所有唯一值,所以我留下:
[
{"start_time":1540458000000},
{"start_time":1540472400000},
{"start_time":1540486800000}
{"end_time":1540472400000},
{"end_time":1540486800000},
{"end_time":1540501200000},
]
我看使用類似的東西使用groupBy
, pluck
, zipObj
多使用這里的答案 。 但遺憾的是沒有運氣。
一個很好的將是一個ramda函數,無需給定特定的鍵即可工作。
另一個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>
這就是我喜歡使用Ramda的方法:構建一個函數管道,每個函數都進行簡單的轉換。
評論詢問如何生成更像的輸出
{
"start_time": [1540458000000, 1540458000000],
"end_time": [1540472400000, 1540486800000]
}
這應該為相同的輸入做到這一點:
const uniqTimes = pipe(
map(toPairs),
unnest,
groupBy(head),
map(map(last)),
map(uniq)
)
不確定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));
如果所需的屬性未知但出現在所有對象中,則可以將每個對象轉換為對,轉置結果數組,獲取每個數組的唯一值,將它們排除在單個數組中,然后轉換回對象:
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>
如果你知道你想要的屬性,你可以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.