例如,如何转换:
[
"20170101|9,4,4,2,6",
"20170102|0,7,5,6,2"
]
进入
[
{
date:"20170101"
numbers:[9, 4, 4, 2, 6]
sum:25
oddOrEven:'odd',
},
{
date:"20170102",
numbers:[0, 7, 5, 6, 2],
sum:20,
oddOrEven:'even',
}
]
如果使用没有中间变量的lodash会好得多。
答案 0 :(得分:1)
不是一行,但它会让你知道如何去做。
var a =[
"20170101|9,4,4,2,6",
"20170102|0,7,5,6,2"
];
a = _.map(a, function(o){
var split = o.split('|'),
numbers = _.map(split[1].split(','), _.ary(parseInt, 1)),
sum = _.sum(numbers);
return {
date: split[0],
numbers: numbers,
sum: sum,
oddOrEven: (sum % 2 == 0 ? 'even' : 'odd')
}
});
console.log(a);
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
答案 1 :(得分:0)
你可以这样做:
library(data.table)
setDT(df)[!is.na(C), RANK := rank(-C) , B]
df
# A B C RANK
# 1: A V1 1 4.0
# 2: A V2 2 3.5
# 3: A V3 3 3.0
# 4: B V1 5 1.0
# 5: B V2 2 3.5
# 6: B V3 NA NA
# 7: C V1 4 2.0
# 8: C V2 6 2.0
# 9: C V3 7 2.0
#10: D V1 3 3.0
#11: D V2 7 1.0
#12: D V3 8 1.0