使用ramda基于序列逻辑有条件地分割阵列

问题描述 投票:1回答:3

我有一系列的照片,每个都有一个aspectRatio。我想将数组分成不同长度的较小数组,这取决于aspectRatio

const photos = [
  { fluid: { aspectRatio: 1.5 } },
  { fluid: { aspectRatio: 1.5 } },
  { fluid: { aspectRatio: 0.67 } },
  { fluid: { aspectRatio: 0.67 } },
  { fluid: { aspectRatio: 0.67 } },
  { fluid: { aspectRatio: 0.67 } },
  { fluid: { aspectRatio: 1.5 } },
  { fluid: { aspectRatio: 1.5 } },
  { fluid: { aspectRatio: 0.67 } }
]

在这种情况下,我希望默认情况下每2个项目拆分数组,然后连续3个aspectRatio < 1项目时拆分为3个项目。

我想留下的是:

const result = [
  [
    { fluid: { aspectRatio: 1.5 } }, 
    { fluid: { aspectRatio: 1.5 } }
  ],
  [
    { fluid: { aspectRatio: 0.67 } },
    { fluid: { aspectRatio: 0.67 } },
    { fluid: { aspectRatio: 0.67 } },
  ],
  [
    { fluid: { aspectRatio: 0.67 } }, 
    { fluid: { aspectRatio: 1.5 } }
  ],
  [
    { fluid: { aspectRatio: 1.5 } }, 
    { fluid: { aspectRatio: 0.67 } }
  ],
]

我已经看过使用splitWhen,但它似乎只能在当前项目上工作,而无法在数组中向前看。

javascript ramda.js
3个回答
1
投票

我认为Ramda在这方面没有任何帮助。我的想法是递归地做这件事:

const group = (ps, rs = []) => ps.length == 0
  ? rs
  : ps.length < 3
    ? rs.concat([ps])
    : ps.slice(0, 3).every(p => p.fluid.aspectRatio < 1)
      ? group(ps.slice(3), rs.concat([ps.slice(0, 3)]))
      : group(ps.slice(2), rs.concat([ps.slice(0, 2)]))

const photos = [{fluid: {aspectRatio: 1.5}}, {fluid: {aspectRatio: 1.5}}, {fluid: {aspectRatio: 0.67}}, {fluid: {aspectRatio: 0.67}}, {fluid: {aspectRatio: 0.67}}, {fluid: {aspectRatio: 0.67}}, {fluid: {aspectRatio: 1.5}}, {fluid: {aspectRatio: 1.5}}, {fluid: {aspectRatio: 0.67}}]

console.log(group(photos))

2
投票

您可以使用Array.reduce()迭代数组,并根据您提供的规则向累加器添加新的子数组。始终将当前项目推送到最后一个子阵列:

const photos = [{"fluid":{"aspectRatio":1.5}},{"fluid":{"aspectRatio":1.5}}, {"fluid":{"aspectRatio":1.5}},{"fluid":{"aspectRatio":0.67}},{"fluid":{"aspectRatio":0.67}},{"fluid":{"aspectRatio":0.67}},{"fluid":{"aspectRatio":0.67}},{"fluid":{"aspectRatio":1.5}},{"fluid":{"aspectRatio":1.5}},{"fluid":{"aspectRatio":0.67}}]

const allAspectsUnder1 = arr => arr.every(o => o.fluid.aspectRatio < 1)

const result = photos.reduce((r, o) => {
  const last = r[r.length - 1]

  if (!last ||
    last.length === 3 ||
    (last.length === 2 && !allAspectsUnder1(last))
  ) r.push([])

  r[r.length - 1].push(o)

  return r
}, [])

console.log(result)

这是Ramda中的等价思想:

const { pipe, length, equals, allPass, any, pathSatisfies, lt, last, anyPass,  isNil, reduce, ifElse, init } = R

const lengthEquals = len => pipe(length, equals(len))

const length2AndLargeAspects = allPass([
  lengthEquals(2),
  any(pathSatisfies(lt(1), ['fluid', 'aspectRatio'])),
])

const shouldAddSub = pipe(
  last,
  anyPass([
    isNil,
    length2AndLargeAspects,
    lengthEquals(3),
  ])
)

const fn = reduce(ifElse(
  shouldAddSub, 
  (a, o) => [...a, [o]],
  (a, o) => [...init(a), [...last(a), o]],
), [])

const photos = [{"fluid":{"aspectRatio":1.5}},{"fluid":{"aspectRatio":1.5}}, {"fluid":{"aspectRatio":1.5}},{"fluid":{"aspectRatio":0.67}},{"fluid":{"aspectRatio":0.67}},{"fluid":{"aspectRatio":0.67}},{"fluid":{"aspectRatio":0.67}},{"fluid":{"aspectRatio":1.5}},{"fluid":{"aspectRatio":1.5}},{"fluid":{"aspectRatio":0.67}}]

const result = fn(photos)

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

1
投票

使用简单的do/while循环。将子数组切成3并检查是否有大于1,如果存在,则返回2

let res=[], sIdx = 0;

do {

 const arr = photos.slice(sIdx, sIdx + 3); 
 if(arr.length > 2 && arr.some(({ fluid:{aspectRatio}}) => aspectRatio >= 1 ) ){
    arr.pop();
 }
 res.push(arr);
 sIdx += arr.length;
 
} while ( sIdx < photos.length)

console.log(res)
.as-console-wrapper {	max-height: 100%!important;}
<script>

const photos = [
  { fluid: { aspectRatio: 1.5 } },
  { fluid: { aspectRatio: 1.5 } },
  { fluid: { aspectRatio: 0.67 } },
  { fluid: { aspectRatio: 0.67 } },
  { fluid: { aspectRatio: 0.67 } },
  { fluid: { aspectRatio: 0.67 } },
  { fluid: { aspectRatio: 1.5 } },
  { fluid: { aspectRatio: 1.5 } },
  { fluid: { aspectRatio: 0.67 } }
]

</script>
© www.soinside.com 2019 - 2024. All rights reserved.