我有一组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
和类似的东西,以及使用answer here的东西。但不幸的是没有运气。
一个很棒的功能是可以在无需指定特定键的情况下运行的ramda函数。
答案 0 :(得分:4)
另一种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)
)
答案 1 :(得分:2)
不确定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));
答案 2 :(得分:2)
如果所需的属性未知,但出现在所有对象中,则可以将每个对象转换成对,转置结果数组,获取每个数组的唯一值,将它们嵌套到单个数组,然后转换回对象:
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>