I would like to get just the "true" checkboxes, to insert then into a my db.
I have an array of categories:
categories_all: [
{
cat_key: "catfood"
at_name: "catfood"
id: 2
kindof: "food"
label: "Cat Food"
},
ect... ]
I map them in checkboxes:
const [checkedItems, setCheckedItems] = useState({})
const handleCheckChange = event => {
setCheckedItems({
...checkedItems,
[event.target.name]: event.target.checked,
})
}
return (
<div>
{categories_all.map(item => (
<label key={item.cat_key}>
{item.label}{" "}
<Checkbox
name={item.id} // I need the id not the name
checked={checkedItems[item.id]}
value={item.value}
onChange={handleCheckChange}
/>{" "}
</label>
))}
</div>
)}
if I console.log(checkedItems) I get this:
{2: false, 3: true, 4: true} // this is just the id not the name
I would like to get something like:
{3, 4}
To insert into a table
How can I do it?