[英]how to add a specific number of items as long as the condition is true JavaScript Arrays
I'm working on column selection from a table.我正在从表格中选择列。 I have a limit of 20 elements.
我有 20 个元素的限制。 I need to be able to select all items until the limit is passed.
我需要能够选择所有项目,直到超过限制。
Scenario: 18/20情景:18/20
What I've tried:我试过的:
const myArray = selectedColumn.some(() => itemsOnArray.length <= 20)
? selectedLaborer.map((laborer) => laborer)
: [];
Can you provide more context to your question?您能否为您的问题提供更多背景信息?
Personally when it comes to selections, i usually gives an id to the object i wish to select if they don't have one (An unique identifier) and store this value in an array as my selected objects.就个人而言,当涉及到选择时,我通常会为我希望选择的对象提供一个 id(如果它们没有)(唯一标识符),并将该值作为我选择的对象存储在一个数组中。
Adding and removing values is quite easy then :添加和删除值非常容易:
(This is typescript for more explicit reading, but doesnt change anything) (这是用于更明确阅读的打字稿,但不会改变任何东西)
let selectedItems: string[] = [];
const MAX_ALLOWER_SELECTED = 20;
const selectItem = (itemId: string): void => {
//You might want to check for duplicates before inserting
if (selectedItems.length > MAX_ALLOWED_SELECTED)
return;
selectedItems.push(itemId);
}
const unselectItem = (itemId: string): void => {
selectedItems = selectedItems.filter(id => itemId !== id);
}
const isItemSelected = (itemId: string): boolean => {
return selectedItems.includes(itemId);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.