I need to create this kind of object when check the checkboxes
{
"targets": [
{
"kind": "foo",
"channelId": "-897",
"quote": "true",
"gatewayName": "bbb"
},{
"kind": "bar",
"channelId": "-1123",
"quote": "true",
"gatewayName": "aaa"
}
]
}
and I have this snippet for my from
{gateways ? gateways.map((g) => {
return (
<Tooltip
key={g.name}
content={`${g.name} - ${g.kind}`}
>
<Switch
key={g.name}
id={g.name}
label={g.name}
name={g.name}
value={`{"kind":"${g.kind}","channelId":"${g.channelId}","gatewayName":"${g.name}","quote":"true"}`}
labelProps={{
className: "text-sm font-normal text-blue-gray-500",
}}
onChange={handleCheckboxChange}
/>
</Tooltip>
)
}) : "Loading..."}
I create the state
const [checkboxes, setCheckboxes] = useState([]);
I'm trying to create the handleCheckboxChange
const handleCheckboxChange = (e) => {
const { name, checked, value } = e.target;
if(checked){
setCheckboxes(prev => ({
targets: [prev.targets,JSON.parse(value)]
}))
}else{
setCheckboxes(prev => ({
targets: prev.filter(target => target !== JSON.parse(value))
}))
}
console.log(checkboxes)
};
But when I check the first checkbox I have two element in array, the first is undefined. when I uncheck I get this error
TypeError: prev.filter is not a function