Reactjs 如何使用消息钩子在数组中推送值
我想在数组中添加状态值。我也尝试使用foreach,但它会出错 我想在数组中添加值,其值为trueReactjs 如何使用消息钩子在数组中推送值,reactjs,Reactjs,我想在数组中添加状态值。我也尝试使用foreach,但它会出错 我想在数组中添加值,其值为true const [values, setValues] = React.useState({ rb: true, metal: true, country: false, international: false, other: false, }); 预期输出为数组[“rb”,“meatal”] 提前感谢。您可以使用Object.keys(values)将
const [values, setValues] = React.useState({
rb: true,
metal: true,
country: false,
international: false,
other: false,
});
预期输出为数组[“rb”,“meatal”]
提前感谢。您可以使用
Object.keys(values)
将键列表放入数组中,然后根据键的值对其进行过滤
Object.keys(values).filter(key => values[key])
这将为您提供数组中为真的键