Javascript 通过从数组值中仅选择真值来更改值

Javasript Changing values ​by selecting only true values ​from among array values

我想把按钮onChange发生时AdditionalShipping的值只修改为additionalShippingArray中数组值中select为true的值

console.log(additionalShippingArray)

0: {select: false, key: 1, AdditionalShipping: 0}
1: {select: true, key: 2, AdditionalShipping: 0}
2: {select: false, key: 3, AdditionalShipping: 0}

仅当 additionalShippingCost 值为 additionalShippingArray 时,我才想在 AdditionalShipping 中添加 5000 select true。

const [additionalShippingCost, setAdditionalShippingCost] = useState<number>(5000);

我希望按钮处于 onClick 状态,并在调用 onClickBatchApplication 函数时应用。我该怎么办?

  const onClickBatchApplication = () => {
    
  };

   return (
     <button onClick={onClickBatchApplication}>apply</button>
)

我认为这可能有效

  const onClickBatchApplication = () => {
    setAdditionalShippingArray(prev => prev.map((i)=> {
    if(i.select) {
    return {...i, AdditionalShipping :additionalShippingCost }
    }else{
    return i
    }
    })

  };