使用Ramda减少“位置”属性

时间:2019-01-22 10:22:49

标签: javascript functional-programming ramda.js

使用此对象:

const state = {
  a: {position: 1},
  b: {position: 2},
  c: {position: 3},
  d: {position: 4},
  e: {position: 5},
}

我想删除b对象,然后从位置较高的对象中将position减少1(因此只需cd和{ {1}})。结果状态应为:

e

在与Ramda一起玩耍之后,我最终得到了这样的东西:

{
  a: {position: 1},
  c: {position: 2},
  d: {position: 3},
  e: {position: 4},
}

它可以工作,但是我并不完全满意,因为我觉得它有点冗长。您认为有更好的书写方式吗?

4 个答案:

答案 0 :(得分:3)

我可能会做这样的事情:

const {dissoc, map} = R

const reshape = (state, key, pos = state[key].position) => dissoc(key, map(
  ({position}) => position > pos ? {position: position - 1} : {position},
  state
))

const state = { a: { position: 1 }, b: { position: 2 }, c: { position: 3 }, d: { position: 4 }, e: { position: 5 }, };

console.log(reshape(state, 'b'))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>

请注意,Nina和customcommander之间关于删除的讨论与此相关。此版本不会更改您的原始版本。 Ramda提出了永远不要这样做的观点。

如果您的状态值不只是{position: ?},则此版本应该可以解决问题:

const reshape = (state, key, pos = state[key].position) => dissoc(key, map(
  ({position, ...rest}) => position > pos 
    ? {position: position - 1, ...rest} 
    : {position, ...rest},
  state
))

或者,正如Emi所指出的那样,此版本的性能可能更高:

const reshape = (state, key, pos = state[key].position) => dissoc(key, map(
  (obj) => obj.position > pos ? {...obj, position: obj.position - 1} : obj,
  state
))

答案 1 :(得分:0)

只需过滤和更新。然后删除。

const state = { b: { position: 2 }, c: { position: 3 }, d: { position: 4 }, e: { position: 5 }, a: { position: 1 } };

Object
    .values(state)
    .filter(o => o.position > state.b.position)
    .forEach(o => o.position--);

delete state.b;

console.log(state);

答案 2 :(得分:0)

使用lenses的ramda解决方案:

const { pipe, dissoc, lensProp, map, over, dec, gt, view, when, flip, prop } = R

const pLens = lensProp('position');

const removeElement = (state, elementId) => pipe(
  dissoc(elementId),
  map(when(
    pipe(
      view(pLens), 
      flip(gt)(view(pLens, prop(elementId, state))
    )), 
    over(pLens, dec)
  ))
)(state)

const state = {"a":{"position":1},"b":{"position":2},"c":{"position":3},"d":{"position":4},"e":{"position":5}}

const result = removeElement(state, 'b')

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>

答案 3 :(得分:0)

我会稍微拆分一些东西:

首先,一个函数返回一个函数,该函数检查{position}对象的position属性是否小于给定数字:

const positionLowerThan = (num) => propSatisfies(lt(num), 'position');

第二,该函数将使用一个{position}对象,并将简单地减少其position属性:

const decreasePosition = over(lensProp('position'), dec);

将所有内容放在一起:

const {propSatisfies, over, lensProp, dec, pipe, omit, map, when, lt} = R;

const positionLowerThan = (num) => propSatisfies(lt(num), 'position');
const decreasePosition = over(lensProp('position'), dec);

const execute = (key, obj) =>
  pipe(omit([key]), map(when(positionLowerThan(obj[key].position), decreasePosition)))
    (obj);

console.log(

  execute('b', {
    a: {position: 1},
    b: {position: 2},
    c: {position: 3},
    d: {position: 4},
    e: {position: 5},
  })

);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>