我们在项目中使用的是ramda,我确实有此数据结构,因此我需要做以下事情:
对于每个集合,检查其中是否包含submittedCheckboxes
并将其隔离到新数组中,以便我知道是否未超过设置为valid_amount
的{{1}}复选框的数量。 / p>
在下面的示例中,它应引发错误,指示集#1的ID,因为true
的两个具有submittedCheckboxes
,其中集指定value: true
,而集#2应该很好valid_amount选项为1或2(valid_amount: [1]
)。
最后一组应接受属于它的所有已选中复选框。
valid_amount: [1,2]
答案 0 :(得分:1)
const sets = [
{id: 1, checkboxes: [{id: 10}, {id: 20}], valid_amount: [1]},
{id: 2, checkboxes: [{id: 30}, {id: 40}], valid_amount: [1,2]},
{id: 3, checkboxes: [{id: 50}, {id: 60}, {id: 70}, {id: 80}], valid_amount: [1,2,3,4]},
];
const submittedCheckboxes = [
{id: 10, value: true},
{id: 20, value: true},
{id: 30, value: true},
{id: 40, value: true},
{id: 50, value: true},
{id: 60, value: false},
{id: 70, value: true},
{id: 80, value: false},
];
console.log(sets.map(valid));
function valid(set) {
return set.valid_amount.includes(set.checkboxes.filter(checkbox => submittedCheckboxes.find(set=>set.id === checkbox.id).value).length)
}
答案 1 :(得分:0)
我认为先重新处理您的数据很有意义:
设置
checkboxes
不必是对象列表(例如[{id:1},{id:2}]
)。它可以只是一个ID列表(例如[1,2]
)如此变换:
{id: 2, checkboxes: [{id: 30}, {id: 40}], valid_amount: [1,2]},
进入:
{id: 2, checkboxes: [30, 40], max_amount: 2}
有一个功能:
const transformSet = set => ({
id: set.id,
checkboxes: map(prop('id'), set.checkboxes),
max_amount: last(set.valid_amount)
});
复选框
(如果我错了,请纠正我)您似乎只关心value
设置为true
的对象。在这种情况下,我会删除将value
设置为false
的那些,并仅保留ID。
所以变换
[
{id: 10, value: true},
{id: 20, value: true},
{id: 30, value: true},
{id: 40, value: true},
{id: 50, value: true},
{id: 60, value: false},
{id: 70, value: true},
{id: 80, value: false},
]
进入:
[10, 20, 30, 40, 50, 70]
有一个功能:
const transformCheckboxes = into([], compose(
reject(propEq('value', false)),
map(prop('id'))
));
为什么?
让我们以第1组为例,查找是否已提交答案就像查找两个数组的交集一样简单:
交集([10,20],[10,20,30,40,50,70]) // => [10,20]
然后,您需要确保数组的长度不大于该集合的最大数量:
const selectCheckboxes = curry((checkboxes, set) =>
pipe(intersection, length, gte(set.max_amount))
(set.checkboxes, checkboxes));
现在,您可以遍历集合,并返回第一个集合,其中selectCheckboxes
的返回值不为true。 (请注意使用complement(selectCheckboxes)
:
const findError = useWith(find, [
compose(complement(selectCheckboxes), transformCheckboxes),
map(transformSet)]);
const {curry, into, intersection, length, gte, map, prop, reject, last, useWith, find, compose, propEq, complement, pipe} = R;
const sets = [
{id: 1, checkboxes: [{id: 10}, {id: 20}], valid_amount: [1]},
{id: 2, checkboxes: [{id: 30}, {id: 40}], valid_amount: [1,2]},
{id: 3, checkboxes: [{id: 50}, {id: 60}, {id: 70}, {id: 80}], valid_amount: [1,2,3,4]},
];
const submittedCheckboxes = [
{id: 10, value: true},
{id: 20, value: true},
{id: 30, value: true},
{id: 40, value: true},
{id: 50, value: true},
{id: 60, value: false},
{id: 70, value: true},
{id: 80, value: false},
];
const transformSet = set => ({
id: set.id,
checkboxes: map(prop('id'), set.checkboxes),
max_amount: last(set.valid_amount)
});
const transformCheckboxes = into([], compose(
reject(propEq('value', false)),
map(prop('id'))
));
const selectCheckboxes = curry((checkboxes, set) =>
pipe(intersection, length, gte(set.max_amount))
(set.checkboxes, checkboxes));
const findError = useWith(find, [
compose(complement(selectCheckboxes), transformCheckboxes),
map(transformSet)]);
console.log(
findError(submittedCheckboxes, sets)
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>