Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return an array which is not equal to a specific number in ReactJS?

Number = 10;

occupiedBeds = [2, 8];

I want to return numbers from 1 to number not equal to occupiedBeds, that is:

[1, 3, 4, 5, 6, 7, 9, 10]

My code so far:

 const vacantBeds = (number) => {
      const occupiedBeds = patientsData && patientsData.map((patient) => Number(patient.room.slice(-1)[0].bed))
    
      let numArr = [...Array(number).keys()].map((x) => x + 1)
    
      let filtered = numArr.filter((x) => x !== occupiedBeds)
    
      return filtered.map((bed) => (
        <option key={bed} value={bed}>
          {bed}
        </option>
      ))
    }
like image 579
Ahmed Ibrahim Avatar asked Dec 21 '25 20:12

Ahmed Ibrahim


1 Answers

This should work for you

  let filtered = numArr.filter( x => !occupiedBeds.includes(x))
like image 169
Udit Avatar answered Dec 23 '25 09:12

Udit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!