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

Viewed 42

I want to change the value of AdditionalShipping when button onChange occurs only to the values ​​in which select is true among the array values ​​in additionalShippingArray.

console.log(additionalShippingArray)

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

I want to put 5000 in AdditionalShipping only if the additionalShippingCost value is additionalShippingArray select true .

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

I want the button to be onClick and applied when the onClickBatchApplication function is called. What should I do?

  const onClickBatchApplication = () => {
    
  };

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

I think this might work

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

  };

Related