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:
我正在使用地图来存储我的复选框的状态。我当前的组件有3个部分。在每个部分,我都有复选框。我想在默认情况下设置这些值:
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
这是一个现场演示
更多回答
优秀答案推荐
You can aggregate the values in the array into a Map
based on the post label.
您可以将数组中的值聚合到基于POST标签的Map中。
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()));
更多回答
我是一名优秀的程序员,十分优秀!