Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set values by default using checkboxes?

I’m using a Map to store the state of my checkboxes. My current component has 3 sections. In each section I have checkboxes. I would like to set these values by default:

const defaultOptions = [
    { label: "Mark", value: "mark" },
    { label: "Steve", value: "steve" },
    { label: "Paul", value: "Paul" }
];

How can I use this array and set values by default? Thanks in advance!

Here’s a live DEMO

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can aggregate the values in the array into a Map based on the post label.

setCheckboxStates(defaultOptions.reduce((acc, curr) => {
  const key = someJson.find(p => p.fields.some(f => f.name === curr.label)).label;
  if (!acc.has(key)) acc.set(key, []);
  acc.get(key).push(curr);
  return acc;
}, new Map()));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading