3

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>
      ))
    }
5
  • But x is a number e.g. 3 and occupiedBeds is array [2, 8] How do I compare these two which are not same type. Commented May 30, 2021 at 15:27
  • ah, sorry, misread the question. So, given number=10 and beds=[2,8], you want to return [1, 3,4,5,6,7, 9,10] - is that correct? Commented May 30, 2021 at 15:29
  • Yes That is what I want Commented May 30, 2021 at 15:31
  • 2
    Then just use numArr.filter((x) => !occupiedBeds.includes(x)) Commented May 30, 2021 at 15:32
  • Yes. Thank you this one solved my problem. Thanks million times Commented May 30, 2021 at 15:35

1 Answer 1

2

This should work for you

  let filtered = numArr.filter( x => !occupiedBeds.includes(x))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.