如何使用配置对数组进行排序

how to you sort a array using a config

如何在 Ramda 上使用配置值对数组文件进行排序 我试过使用 R.map 然后添加一个条件 R.includes 如果它是真的 return 值 R.always 但我一直在获取 [ [Function], [Function], [Function] ]

我使用的代码

args = [ 'BBQ', 'Tuna', 'Cake', 'Wood' ]

config = [ { foodType: 'Tuna' },
           { foodType: 'BBQ' },
           { foodType: 'Cake' } ]

const getPriority = R.curry((config, args) =>
  R.pipe(
    R.map(
      R.pipe(
        R.prop('foodType'),
          R.ifElse(
           R.includes(args),
           R.always
        )
      )
    ),
    R.flatten,
  )(config)
);

Expected result is [ 'Tuna', 'BBQ', 'Cake' ]

知道为什么我一直在获取 [[Function]、[Function]、[Function] ]

我想根据我看到的代码来猜测您在这里的要求。在我看来,您想要 config 中的 foodTypes,它们也可以在 args 中找到,并按它们在 config 中出现的顺序排序。是吗?

如果是这样,这段代码似乎可以工作:

const getPriority = (args) => (config) =>
  config .reduce ((a, {foodType}) => args.includes(foodType) ? [...a, foodType] : a, [])

const args = [ 'BBQ', 'Tuna', 'Cake', 'Wood' ]

const config = [ { foodType: 'Tuna' },
                 { foodType: 'Sprouts' }, // added -- not everything is in output
                 { foodType: 'BBQ' },
                 { foodType: 'Cake' } ]

console .log (getPriority (args) (config))

如果我们选择,我们可以为此使用一些 Ramda 函数:

const getPriority = (args) =>
  reduce ((a, {foodType}) => includes (foodType, args) ? append(foodType, a) : a, [])

或者可能是不同的版本:

const getPriority = (args) =>
  pipe(pluck('foodType'), filter(flip(includes)(args)))

...但他们似乎并没有为这个功能增加太多。我是 Ramda 的创始人之一,但我认为它是一种在有帮助时使用的工具,而不是一种新的框架或用于编码的迷你语言。这里好像用处不大。