如何删除findIndexInO
中compareIndexes
的重复来电?
const a = [
{name: 'charlie', value: 1},
{name: 'bravo', value: 7},
{name: 'delta', value: 12},
{name: 'alpha', value: 19},
]
const propEqName = propEq('name');
const findIndexInO = findIndex(__, a);
const compareIndexes = comparator((from, to) => lt(findIndexInO(propEqName(from)), findIndexInO(propEqName(to))))
const sortOnIndex = sort(compareIndexes);
const decideOrder = (...args) => sortOnIndex(args);
decideOrder('delta', 'charlie'); // ['charlie', 'delta']
decideOrder('bravo', 'alpha'); // ['bravo', 'alpha']
decideOrder('alpha', 'charlie'); // ['charlie', 'alpha']
答案 0 :(得分:2)
怎么样
const order = addIndex(reduce)((ord, a, idx) => assoc(a.name, idx, ord), {}, a)
const decideOrder = unapply(sortBy(prop(__, order)))
?因为您已经在函数中嵌入了a
。作为额外的好处,如果对您而言,您也可以这样做:
decideOrder('delta', 'alpha', 'bravo') //=> ["bravo", "delta", "alpha"]