Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

77
Views
React push objects within an array to useState array

I have a problem filling an array. Currently variable terapije contains an object within an array based on the if conditions (i've posted a few, but there's way more). I am using useState hook and i set the default value to an empty array which needs to be filled with terapije objects. Currently terapije is filled but my "terapijaForDropdown" is just an empty array. How can i push an existing array to another ? i've tried multiple solutions and nothing seems to work.


 function onSkupinaZdravilChange () {

        if (form.preparati){

            let skupine = form.preparati.map(p => p.preparati && +catalogue.preparat.display[p.preparati].skupinaZdravil.code)
            let terapije = []
            
            if (skupine.length === 1 && skupine.every(i => [1,2,3,4,5].includes(i))){
                terapije.push(findAttributeByCode(catalogue.terapija, '1'))
            }

            if (skupine.length === 2 && skupine.every(i => [1,2,3,4,5].includes(i))){
                terapije.push(findAttributeByCode(catalogue.terapija, '2'))
            }

            if (skupine.includes(6) || (skupine.length === 2 && skupine.includes(6) && skupine.every(i => [1,2,3,4,5].includes(i)))){
                terapije.push(findAttributeByCode(catalogue.terapija, '3'))
            }
                
            if (skupine.length === 1 && skupine.includes(6)){
                terapije.push(findAttributeByCode(catalogue.terapija, '4'))
            }

   setTerapijaForDropdown([...terapijaForDropdown, terapije]);

}}

const [terapijaForDropdown, setTerapijaForDropdown] = useState([]);
useEffect(() => {
        onSkupinaZdravilChange();
     },[form.preparati])

7 months ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.