Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
Setting state of an array of objects in react js

I am creating a new Array every time an state is changing to display different number of fields on UI.

  const [highestEducation, setHighestEducation] = useState([eduObj]);

  useEffect(() => {
    if (edu === "intermediate") {
      const filled = new Array(2).fill(eduObj);
      setHighestEducation(filled);
    } else if (edu === "graduation") {
      const filled = new Array(3).fill(eduObj);
      setHighestEducation(filled);
    } else if (edu === "postGraduation") {
      const filled = new Array(4).fill(eduObj);
      setHighestEducation(filled);
    } else if (edu === "highSchool") {
      const filled = new Array(1).fill(eduObj);
      setHighestEducation(filled);
    }
  }, [edu]);

I am mapping over the highest education to display input on change of input i have declared a function which sets the state accourding to index in highest education array.

 const onChangeEdu = (e, id) => {
    let value = [...highestEducation];
    value[id][e.target.name] = e.target.value;
    setHighestEducation(value);
  };

but when i change the value of input passing in index and event it updates all object value in highest education array.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!